sushil suthar
sushil suthar

Reputation: 679

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

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?

Upvotes: 2

Views: 7389

Answers (1)

Masoud Andalibi
Masoud Andalibi

Reputation: 3228

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

Upvotes: 3

Related Questions