【问题标题】:Not getting MapToStoredProcedure option in OnModelCreating在 OnModelCreating 中没有获得 MapToStoredProcedure 选项
【发布时间】:2016-05-20 06:55:03
【问题描述】:

我正在使用 Code First 方法使用 MVC 开发 EntityFramework。我正在使用现有的数据库。当我覆盖 OnModelCreating 方法时,我看不到将我的实体映射到存储过程的选项。

有什么理由吗?

谢谢!

附上图片

【问题讨论】:

  • 添加OnModelCreating 方法以询问您是否仍有问题 - 请详细说明。
  • 您使用的是什么版本的 MVC 和 EF?
  • 认为您需要升级到 EF6 - visualstudiomagazine.com/articles/2014/03/01/…
  • 嘿,谢谢。我没有注意到这一点。该项目有 EF 4.0。我已经升级了 EF,现在我得到了那个方法。

标签: sql asp.net asp.net-mvc entity-framework


【解决方案1】:

只需将您的实体框架版本 5 更新到 6 或更高版本,因为 MapToStoredProcedures 方法在 EF5 中不支持

【讨论】:

  • 那么 EF 核心中 MapToStoredProcedures() 的等效方法是什么
  • 你可以使用FromSqlRaw
  • 那么在 EF 核心中没有更多的 API 用于等效的 MapToStroedProcedures 了吗?对吗?
【解决方案2】:

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

【讨论】:

  • 我也做过同样的事情。我的问题是我没有在智能感知中获得 MapToStoredProcedures 选项。我已将图片附加到帖子中。
猜你喜欢
  • 2015-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-09
  • 1970-01-01
相关资源
最近更新 更多