Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using stored procedures with Entity Framework 4 Code First CTP5

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?

like image 356
Brendan Vogt Avatar asked Feb 15 '11 08:02

Brendan Vogt


People also ask

How do you call a stored procedure in Entity Framework first?

Open the SchoolModel. Store node and then open the Stored Procedures node. Then right-click the GetCourses stored procedure and select Add Function Import. In the Add Function Import dialog box, under Returns a Collection Of select Entities, and then select Course as the entity type returned.

Can we use Entity Framework with stored procedure?

Entity Framework has the ability to automatically build native commands for the database based on your LINQ-to-Entities or Entity SQL queries, as well as build the commands for inserting, updating, and deleting data. You may want to override these steps and use your own predefined stored procedures.


1 Answers

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));
like image 100
Ladislav Mrnka Avatar answered Sep 23 '22 01:09

Ladislav Mrnka