问题
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>("storedProcedureName",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