原文链接: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. RFC2616-HTTP1.1-Header Field Definitions(头字段规定部分—单词注释版)

    part of Hypertext Transfer Protocol -- HTTP/1.1RFC 2616 Fielding, et al. 14 Header Field Definitions ...

  2. 在cikuapi.com上抓取相关词

    最近用到文本相关性计算,要在开放域语料上操作,找了好久没找到好的方法,后来看到了清华的梁斌老师建的cikuapi,上面能查询一些相关词,自己写代码爬的时候出现中文解码问题,遂到Github上找了下相关 ...

  3. JSONP原理实现及url传递参数封装

      利用在页面中创建<script>节点的方法向不同域提交HTTP请求的方法称为JSONP,这项技术可以解决跨域提交Ajax请求的问题. JSONP的优点是:它不像XMLHttpReque ...

  4. BZOJ.5288.[AHOI/HNOI2018]游戏(思路 拓扑)

    BZOJ LOJ 洛谷 考虑如何预处理每个点能到的区间\([l,r]\). 对于\(i,i+1\)的一扇门,如果钥匙在\(i\)的右边,连边\(i\to i+1\),表示从\(i\)出发到不了\(i+ ...

  5. CentOS7.5 通过wget下载文件到指定目录

    在Linux命令行下面下载文件,通过wget是比较普遍简单的,比如在CentOS7 里面也一样. 我们先来看下自己的CentOS7 系统有没有安装wget: [root@test redis]# rp ...

  6. 解决VS Code开发Python3语言自动补全功能不带括号的问题

    Visual Studio Code(以下简称VS Code)用来开发Python3,还是很便利的,本身这个IDE就是轻量级的,才几十兆大小,通过安装插件的方式支持各种语言的开发.界面也美美哒,可以在 ...

  7. 寻找总是出错的R文件

    寻找R文件 相信大家使用Android studio开发APP时最常见的一个问题就是R文件爆红了·· 接下来我们来看看怎么寻找R文件 首先我们得从Android转换成Project. 然后就是依次点击 ...

  8. 64位ubuntu搭建android开发环境问题解决方案

    安装32位库支持,删除eclipse 的配置文件和.android目录(测试环境ubuntu 14.04) sudo apt-get install libc6-i386 lib32stdc++6 l ...

  9. 小甲鱼Python第十讲课后题---

    0. 下边的列表分片操作会打印什么内容? >>> list1 = [1, 3, 2, 9, 7, 8]>>> list1[2:5] [2,9,7] 1.请问 lis ...

  10. PHP07

    PHP07 1.cookie 2.使用php操作cookie 设置响应头(header)中的Set-Cookie可以下发小票 检查-network-响应头处可查看所设置cookie 检查-applic ...