Question:
I have a project that uses the Entity Framework without an edmx
model, we register the entities manually, create a class and insert it in the context.
Does anyone use this format and know how to register a Stored Procedure this way?
Answer:
From version 5 of the Entity Framework there is a simple way to execute a Stored Procedure based on the DbContext
instance, using DataBase
property, see:
using(MeuContexto context = new MeuContexto())
{
SqlParameter param = new SqlParameter("@idCliente", 1);
context.Database.ExecuteSqlCommand("sp_ExcluirCliente @idCliente", param);
}
Or even run Stored Procedures with return, whose return can be an entity of its context:
public List<Cliente> Clientes()
{
using(MeuContexto context = new MeuContexto())
{
return context.Database.SqlQuery<Cliente>("exec sp_Clientes).ToList();
}
}