我正在使用Code First方法使用MVC处理EntityFramework。我正在使用现有的数据库。当我重写OnModelCreating方法时,我没有看到将我的实体映射到存储过程的选项。
有什么理由?
谢谢!
答案 0 :(得分:0)
请参阅here以获取参考:
以下示例自动为其创建存储过程 使用Fluent API的学生实体。
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Student>().MapToStoredProcedures();
}
上面显示的代码将创建三个程序Student_Insert, Student_Update和Student_Delete。 Student_Insert和Student_Update 存储过程具有与之对应的参数名称 财产名称。 Student_Delete将具有主键属性 StudentID参数。
您还可以更改存储过程和参数名称,如下所示:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Student>()
.MapToStoredProcedures(p => p.Insert(sp => sp.HasName("sp_InsertStudent").Parameter(pm => pm.StudentName, "name").Result(rs => rs.Student_ID, "Student_ID"))
.Update(sp => sp.HasName("sp_UpdateStudent").Parameter(pm => pm.StudentName, "name"))
.Delete(sp => sp.HasName("sp_DeleteStudent").Parameter(pm => pm.Student_ID, "Id"))
);
}
答案 1 :(得分:0)
只需将您的实体框架版本5更新为6或更高版本,因为Map5中不支持MapToStoredProcedures方法