Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run stored procedures in Entity Framework Core?

I am using EF Core 1.0 in an ASP.NET Core App. Can you please point me to the proper way of executing stored procedures? The old method with ObjectParameters and ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction is not working.

like image 221
eadam Avatar asked Feb 19 '15 05:02

eadam


People also ask

Can we call stored procedure in Entity Framework Core?

Stored procedures are one of the key advantages that the Microsoft SQL server provides. For boosting the query performance, the complex query should be written at the database level through stored procedures. Microsoft . NET Core supports calling of raw query and store procedure through entity framework.

Can you use stored procedures with Entity Framework?

The Entity Framework has the capability of importing a Stored Procedure as a function. We can also map the result of the function back to any entity type or complex type.

How can use stored procedure in asp net core Entity Framework?

The support for stored procedure in EF Core is similar to the earlier versions of EF Code first. You need to create your DbContext class by inherting the DbContext class from EF. The stored procedures are executing using the DbContext. First step is to write a method that create a DbCommand from the DbContext.


1 Answers

Support for stored procedures in EF Core 1.0 is resolved now, this also supports the mapping of multiple result-sets.

Check here for the fix details

And you can call it like this in c#

var userType = dbContext.Set().FromSql("dbo.SomeSproc @Id = {0}, @Name = {1}", 45, "Ada"); 
like image 87
Arvin Avatar answered Sep 17 '22 15:09

Arvin