Entity Framework Core 2.0 call stored procedure

c# entity-framework-core

Question

How can I call a stored procedure using Entity Framework Core 2.0? Asp.net core 2.0 site in C#

1
2
11/28/2018 10:42:50 AM

Accepted Answer

Support for stored procedure in EF7 is resolved now, this also supports 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");
67
12/16/2019 3:49:24 PM

Popular Answer

Stored procedure support is not yet (as of 7.0.0-beta3) implemented in EF7. You can track the progress of this feature using issue #245.

For now, you can do it the old fashioned way using ADO.NET.

var connection = (SqlConnection)context.Database.AsSqlServer().Connection.DbConnection;

var command = connection.CreateCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "MySproc";
command.Parameters.AddWithValue("@MyParameter", 42);

command.ExecuteNonQuery();


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow