原文链接:https://www.entityframeworktutorial.net/entityframework6/code-first-insert-update-delete-stored-procedure-mapping.aspx

EF 6 Code-First系列文章目录:

当SaveChanges方法被调用的时候,EF 6  可以用来创建并使用增删改存储过程。

我们来为下面的Student实体,创建增删改存储过程。

class Student
{
public int StudentId { get; set; }
public string StudentName { get; set; }
public DateTime DoB { get; set; }
}

使用MapToStoredProcedures()方法,为实体配置默认的存储过程。

public class SchoolContext: DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Student>()
.MapToStoredProcedures();
} public DbSet<Student> Students { get; set; }
}

EF API将会为Student实体创建Student_InsertStudent_Update 和Student_Delete存储过程。

Student_Insert和Student_Update存储过程包含Student实体的所有属性的参数,Student_Delete存储过程仅仅包含Student的主键属性StudentID一个参数:

CREATE PROCEDURE [dbo].[Student_Insert]
@StudentName [nvarchar](max),
@DoB [datetime]
AS
BEGIN
INSERT [dbo].[Students]([StudentName], [DoB])
VALUES (@StudentName, @DoB) DECLARE @StudentId int
SELECT @StudentId = [StudentId]
FROM [dbo].[Students]
WHERE @@ROWCOUNT > AND [StudentId] = scope_identity() SELECT t0.[StudentId]
FROM [dbo].[Students] AS t0
WHERE @@ROWCOUNT > AND t0.[StudentId] = @StudentId
END CREATE PROCEDURE [dbo].[Student_Update]
@StudentId [int],
@StudentName [nvarchar](max),
@DoB [datetime]
AS
BEGIN
UPDATE [dbo].[Students]
SET [StudentName] = @StudentName, [DoB] = @DoB
WHERE ([StudentId] = @StudentId)
END CREATE PROCEDURE [dbo].[Student_Delete]
@StudentId [int]
AS
BEGIN
DELETE [dbo].[Students]
WHERE ([StudentId] = @StudentId)
END

为实体映射自定义的存储过程

EF6允许你使用自己的存储过程,你可以像下面这样进行配置,下面的代码为Student实体,映射了一个自定义的存储过程。

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.StudentId, "Id"))
.Update(sp => sp.HasName("sp_UpdateStudent").Parameter(pm => pm.StudentName, "name"))
.Delete(sp => sp.HasName("sp_DeleteStudent").Parameter(pm => pm.StudentId, "Id"))
);
}

在上面的例子中,Student实体映射了三个存储过程,sp_InsertStudent、sp_UpdateStudent、以及sp_DeleteStudent.当然同样对存储过程的参数进行了配置。

为所有实体配置存储过程

你可以使用下面的代码,为所有实体配置存储过程。

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Types().Configure(t => t.MapToStoredProcedures());
}

局限性

  • 仅仅只有Fluent API才能被用来映射存储过程。EF 6中的数据注解特性,是不能映射存储过程的。
  • 如果你想使用CUD操作,你就必须为实体映射Insert,Update以及Delete存储过程。仅仅是映射其中一个,是不被允许的。

16.翻译系列:EF 6 Code -First中使用存储过程【EF 6 Code-First系列】的更多相关文章

  1. 9.2 翻译系列:数据注解特性之---Column【EF 6 Code First系列】

    原文链接:http://www.entityframeworktutorial.net/code-first/column-dataannotations-attribute-in-code-firs ...

  2. 21.翻译系列:Entity Framework 6 Power Tools【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/entity-framework-power-tools.aspx 大家好,这里就是EF ...

  3. 9.10 翻译系列:EF数据注解特性之StringLength【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/stringlength-dataannotations-attribute-in-co ...

  4. 9.9 翻译系列:数据注解特性之--MaxLength 【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/maxlength-minlength-dataannotations-attribut ...

  5. 9.7 翻译系列:EF数据注解特性之--InverseProperty【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/inverseproperty-dataannotations-attribute-in ...

  6. 9.3 翻译系列:数据注解特性之Key【EF 6 Code-First 系列】

    原文链接:http://www.entityframeworktutorial.net/code-first/key-dataannotations-attribute-in-code-first.a ...

  7. 9.1 翻译系列:数据注解特性之----Table【EF 6 Code-First 系列】

    原文地址:http://www.entityframeworktutorial.net/code-first/table-dataannotations-attribute-in-code-first ...

  8. 9.8 翻译系列:数据注解特性之--Required 【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/required-attribute-dataannotations-in-code-f ...

  9. 9.11 翻译系列:数据注解特性之--Timestamp【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/TimeStamp-dataannotations-attribute-in-code- ...

随机推荐

  1. c++基本数据类型及其取值范围

    #include<iostream> #include<string> #include <limits> using namespace std; int mai ...

  2. Java内存空间的分配及回收

    Java中内存分为: 栈:存放简单数据类型变量(值和变量名都存在栈中),存放引用数据类型的变量名以及它所指向的实例的首地址. 堆:存放引用数据类型的实例. Java的垃圾回收 由一个后台线程gc进行垃 ...

  3. AS安装过程中出现的错误

    1.首先是You may need to adjust the proxy settings in Gradle.的错误, 主要是看你有没有图中红线所画的gradle的压缩包 如果没有,那就前往htt ...

  4. C#相关FTP操作

    C# 相关 FTP(File Transfer Protocol) 操作 1.获取文件目录 UsePassive:需要设置为false. 使用被动模式,因为客户端防火墙的存在,所以为了确保数据可以正常 ...

  5. 在Windows系统下安装Beautiful Soup4的步骤和方法

    1.到http://www.crummy.com/software/BeautifulSoup/网站上上下载,最新版本是4.3.2. 2.下载完成之后需要解压缩,假设放到D:/python下. 3.运 ...

  6. JS运算符问题

    以下代码是否报错,如果不报错输出什么,为什么 var x = !!"Hello" + (!"world", !!"from here!!") ...

  7. C#基础用户登陆

    1.主界面代码: 2.注册页面 3.登陆界面 登陆注册代码: //编写登录界面逻辑 using System; using System.Collections.Generic; using Syst ...

  8. LOJ.6074.[2017山东一轮集训Day6]子序列(DP 矩阵乘法)

    题目链接 参考yww的题解.本来不想写来但是他有一些笔误...而且有些地方不太一样就写篇好了. 不知不觉怎么写了这么多... 另外还是有莫队做法的...(虽然可能卡不过) \(60\)分的\(O(n^ ...

  9. AGC 010D.Decrementing(博弈)

    题目链接 \(Description\) 给定\(n\)个数\(A_i\),且这\(n\)个数的\(GCD\)为\(1\).两个人轮流进行如下操作: 选择一个\(>1\)的数使它\(-1\). ...

  10. BZOJ.4559.[JLOI2016]成绩比较(DP/容斥 拉格朗日插值)

    BZOJ 洛谷 为什么已经9点了...我写了多久... 求方案数,考虑DP... \(f[i][j]\)表示到第\(i\)门课,还有\(j\)人会被碾压的方案数. 那么\[f[i][j]=\sum_{ ...