Use Stored Procedure in Ef Core

–Stored Procedure
create proc SpUserInfo
as
BEGIN
select 1 Id,’akash’ Name;

END

and EF core

using (var connection = (SqlConnection)DbContext.Database.GetDbConnection())
{
connection.Open();
var command = connection.CreateCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = “SpUserInfo”;
//command.Parameters.AddWithValue(“@MyParameter”, 42);

var reader = command.ExecuteReader();
while (reader.Read())
{
int id = Convert.ToInt32(reader[“Id”]);
String name = Convert.ToString(reader[“name”]);
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *