使用存储过程与实体框架4代码第一CTP5 [英] Using stored procedures with Entity Framework 4 Code First CTP5

查看:134
本文介绍了使用存储过程与实体框架4代码第一CTP5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我有我的呼吁类中的以下代码:

  MyDatabaseContext db = new MyDatabaseContext(); 

public void Insert(新闻消息)
{
db.Newses.Add(news);
db.SaveChanges();
}

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

  ALTER PROCEDURE [dbo]。[News_Insert] 

@Title VARCHAR(100),
@Body VARCHAR(MAX),
@NewsStatusId INT


AS

BEGIN

INSERT INTO
新闻

标题,
身体,
NewsStatusId

VALUES

@Title,
@Body ,
@NewsStatusId
);

SELECT SCOPE_IDENTITY();

END

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



更新1:



与设计师可以返回新对象或新闻ID,我会这样做吗?



更新2



这是我做的:

  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();
}

我在这里收到错误说:



从实例化的System.Decimal类型到System.Int32类型的指定转换是无效的。



我的新闻类:

  public class News 
{
public int NewsId {get;组; }
public string标题{get;组; }
public string Body {get;组; }
public bool Active {get;组;
}

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

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

这是我的上下文类:

  public class MyDatabaseContext:DbContext 
{
public DbSet< News>新闻{get;组; }
}

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

解决方案

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

  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));


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?

I have the following code in my respository class:

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:

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

UPDATE 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.

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?

解决方案

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:

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));

这篇关于使用存储过程与实体框架4代码第一CTP5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆