How to MAP select stored procedure in Entity Framework 6 code-first approach?

懵懂的女人 提交于 2021-01-18 04:47:08

问题


For example when we write

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<tblError>().MapToStoredProcedures();
}

Then it will create 3 stored procedures in the database with the names of tblError_Delete, tblError_Insert, tblError_Update

My question is about tblError_Select: how to map a stored procedure that is used to execute select queries?

I want EF to be generate tblError_Select along with preceding 3 stored procedures.

I want to do CRUD operation using stored procedures with EF. Currently I can do Insert, Update, Delete but what about Select? Why does EF not create a stored procedure for Select operation?


回答1:


Since this question brought some new insights, I've researched a little and marc_s answered helped. Basically Knowing EF Code-First, when you do not have any database it is kinda impossible to create a SP for Select, since you have to create the database first then insert data within then select what is there (which makes more sense), you have to create the Stored Procedure within the SQL and use code below to run it: You can call a stored procedure in your DbContext class as follows.

this.Database.SqlQuery<YourEntityType>("storedProcedure‌​Name",params);

Credit goes to @Marc_S and Source



来源:https://stackoverflow.com/questions/41312934/how-to-map-select-stored-procedure-in-entity-framework-6-code-first-approach

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!