c# - 如何从实体数据向导导入实体框架代码中的现有存储过程?
问题描述
今天我首先使用现有的数据库方法来模拟代码。但是在 VS 2017 中的实体数据模型向导 -> 将选定的存储过程导入实体模型复选框中的功能被禁用。请看附件截图:
那么如何启用该选项?
解决方案
您不需要导入程序。
代码示例中显示了现有存储过程(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"))
);
}
}
推荐阅读
- android - 修复 android studio 的 android sdk 设置
- python - 如何将从 text() 小部件获得的字符串转换为 int 数据类型列表?
- javascript - 如何在 javascript 和 Node.js 中正确设置类和导出
- python - PyQt5 从 QTextEdit 获取字符格式
- python-3.x - Python - 比较偶数和奇数输入的总和?
- html - Angular 2:如何将图像放在循环内的画布上?
- java - Thymeleaf th:循环中的每个访问参数
- r - 如何对多个样本/列进行 Savitzky-Golay 过滤 (R)
- excel - 用户定义类型变量
- sql - 将减号查询的结果(varchars 列表)存储在 Oracle PL/SQL 中的变量中