EF 6 Code First From Database - Calling existing stored procedure

c# entity-framework entity-framework-6 sql-server

Question

I am using EF 6 to connect to an existing SQL database. I want to generate a model for some existing entities and non-CRUD stored procedures. From visual studio, I am adding a new "ADO.NET Entity Data Model" and selecting "Code First from Database" for the model content. However, the only database objects that are available to me are Tables and Views. Stored procedures aren't available. Is this by design or am i missing something here?

1
4
2/12/2019 5:59:07 AM

Accepted Answer

You need to define a DTO for each stored procedure. You just create simple classes where the propery names of the classes match the name of the columns that are returned by the stored procedure. So, when you call the Stored Procedure from EF, your objects are built and populated. The performance is great! Here I'm pasting an example:

dbContext.Database.CommandTimeout = 3600;
List<CarsDTO> objs = dbContext.Database.SqlQuery<CarsDTO>(
         "spGetCars @carMakeId", new SqlParameter("carMakeId", id)
).ToList();
0
6/24/2015 10:04:38 PM


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