首页 > 解决方案 > 如何从实体数据向导导入实体框架代码中的现有存储过程?

问题描述

今天我首先使用现有的数据库方法来模拟代码。但是在 VS 2017 中的实体数据模型向导 -> 将选定的存储过程导入实体模型复选框中的功能被禁用。请看附件截图:

实体数据模型向导

那么如何启用该选项?

标签: c#visual-studio-2017entity-framework-6

解决方案


您不需要导入程序。

代码示例中显示了现有存储过程(InsertStudent、UpdateStudent)的使用。只需要用OnModelCreating实体覆盖方法和映射存储过程。

请参阅此页面http://www.entityframeworktutorial.net/EntityFramework6/code-first-insert-update-delete-stored-procedure-mapping.aspx了解详细信息。

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"))
        );
   }
}

推荐阅读