简体   繁体   中英

EF 6 Code First From Database - Calling existing stored procedure

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?

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM