使用存储过程与Entity Framework 4 Code First CTP5

时间:2022-05-04 02:14:31

I am using Entity Framework 4 Code First CTP5 and ASP.NET MVC 3. I would like to know how do I make use of stored procedures?

我正在使用Entity Framework 4 Code First CTP5和ASP.NET MVC 3.我想知道如何使用存储过程?

I have the following code in my respository class:

我的respository类中有以下代码:

MyDatabaseContext db = new MyDatabaseContext();

public void Insert(News news)
{
   db.Newses.Add(news);
   db.SaveChanges();
}

And then my insert store procedure will look like this:

然后我的插入存储过程将如下所示:

ALTER PROCEDURE [dbo].[News_Insert]
(
   @Title VARCHAR(100),
   @Body VARCHAR(MAX),
   @NewsStatusId INT
)

AS

BEGIN

   INSERT INTO
      News
      (
         Title,
         Body,
         NewsStatusId
      )
      VALUES
      (
         @Title,
         @Body,
         @NewsStatusId
      );

  SELECT SCOPE_IDENTITY();

END

Any articles/advice would be appreciated.

任何文章/建议将不胜感激。

UPDATE 1:

更新1:

With the designer I could return the new object or news ID, how would I do this here?

有了设计师,我可以返回新对象或新闻ID,我将如何在此处执行此操作?

UPDATE 2

更新2

This is what I did:

这就是我做的:

public void Insert(News news)
{
   int newsId = context.Database.SqlQuery<int>("News_Insert @Title, @Body, @Active",
      new SqlParameter("Title", news.Title),
      new SqlParameter("Body", news.Body),
      new SqlParameter("Active", news.Active)
   ).FirstOrDefault();

   context.SaveChanges();
}

I get an error here saying:

我在这里得到一个错误说:

The specified cast from a materialized 'System.Decimal' type to the 'System.Int32' type is not valid.

从物化的“System.Decimal”类型到“System.Int32”类型的指定强制转换无效。

My News class:

我的新闻课:

public class News
{
   public int NewsId { get; set; }
   public string Title { get; set; }
   public string Body { get; set; }
   public bool Active { get; set; }
}

I am using an existing database and my connection string looks like this:

我正在使用现有数据库,我的连接字符串如下所示:

<connectionStrings>
   <add
      name="MyDatabaseContext"
      connectionString="Data Source=MyServer;Initial Catalog=MyDatabase;Integrated Security=True;"
      providerName="System.Data.SqlClient" />
</connectionStrings>

Here is my context class:

这是我的上下文类:

public class MyDatabaseContext : DbContext
{
   public DbSet<News> Newses { get; set; }
}

I'm not sure what I am doing wrong here? Is my stored procedure maybe incorrect? Ideally I would like to return the new updated object. Can someone please advise?

我不确定我在这里做错了什么?我的存储过程可能不正确吗?理想情况下,我想返回新的更新对象。有人可以建议吗?

2 个解决方案

#1


9  

There is no direct mapping support for stored procedures (as known from EDMX). You can simply call your procedure instead of Add method by using db.Database.SqlCommand method. Try this:

存储过程没有直接映射支持(如EDMX所知)。您可以使用db.Database.SqlCommand方法简单地调用您的过程而不是Add方法。尝试这个:

db.Database.SqlCommand("dbo.News_Insert @Title, @Body, @NewsStatusId",
  new SqlParameter("Title", news.Title),
  new SqlParameter("Body", news.Body),
  new SqlParameter("NewsStatusId", news.NewStatus.Id));

#2


1  

you can write stored procedures in sql and your entity update and then conversion it to function and use it such as function in your program,db-click on the Entity Model and in open page(EntityModel) then i right side page db-click on folder that called StoredproceduresEntity and open then right click on the a StoredProcedures and select option AddFunction... then select type of output go to: http://msdn.microsoft.com/en-us/data/gg699321.aspx

你可以在sql和你的实体更新中编写存储过程,然后将其转换为函数并使用它,例如程序中的函数,db-单击实体模型和打开页面(EntityModel)然后我右侧页面db-click on调用StoredproceduresEntity并打开的文件夹,然后右键单击StoredProcedures并选择AddFunction选项...然后选择输出类型转到:http://msdn.microsoft.com/en-us/data/gg699321.aspx

#1


9  

There is no direct mapping support for stored procedures (as known from EDMX). You can simply call your procedure instead of Add method by using db.Database.SqlCommand method. Try this:

存储过程没有直接映射支持(如EDMX所知)。您可以使用db.Database.SqlCommand方法简单地调用您的过程而不是Add方法。尝试这个:

db.Database.SqlCommand("dbo.News_Insert @Title, @Body, @NewsStatusId",
  new SqlParameter("Title", news.Title),
  new SqlParameter("Body", news.Body),
  new SqlParameter("NewsStatusId", news.NewStatus.Id));

#2


1  

you can write stored procedures in sql and your entity update and then conversion it to function and use it such as function in your program,db-click on the Entity Model and in open page(EntityModel) then i right side page db-click on folder that called StoredproceduresEntity and open then right click on the a StoredProcedures and select option AddFunction... then select type of output go to: http://msdn.microsoft.com/en-us/data/gg699321.aspx

你可以在sql和你的实体更新中编写存储过程,然后将其转换为函数并使用它,例如程序中的函数,db-单击实体模型和打开页面(EntityModel)然后我右侧页面db-click on调用StoredproceduresEntity并打开的文件夹,然后右键单击StoredProcedures并选择AddFunction选项...然后选择输出类型转到:http://msdn.microsoft.com/en-us/data/gg699321.aspx