How to import existing stored procedure in Entity Framework code-first from entity data wizard?

c# entity-framework-6 visual-studio-2017

Question

Today I'm simulating code first with an existing database approach. But on Entity Data Model Wizard in VS 2017 -> Import selected stored procedures an functions into the entity model check box is disabled. Please see attached screenshot:

Entity Data Model Wizard.

So how do I enable that option?

1
0
9/30/2018 6:21:12 AM

Popular Answer

You don't need to import the procedures.

Use of exisitng store procedures(InsertStudent,UpdateStudent) are shown in code sample. Just need to override OnModelCreating method and map store procedures with entities.

Kindly refer to this page http://www.entityframeworktutorial.net/EntityFramework6/code-first-insert-update-delete-stored-procedure-mapping.aspx for detailed information.

public class SchoolContext: DbContext 
{
   protected override void OnModelCreating(DbModelBuilder modelBuilder)
   {

     modelBuilder.Entity<Student>()
        .MapToStoredProcedures(p => p.Insert(sp => sp.HasName("InsertStudent").Parameter(pm => pm.StudentName, "name").Result(rs => rs.StudentId, "Id"))
            .Update(sp => sp.HasName("UpdateStudent").Parameter(pm => pm.StudentName, "name"))
            .Delete(sp => sp.HasName("DeleteStudent").Parameter(pm => pm.StudentId, "Id"))
        );
   }
}
0
10/2/2018 1:16:20 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