原文链接: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. 003.etcd集群部署-静态发现

    一 etcd集群概述 1.1 概述 静态启动etcd集群要求每个成员都知道集群中的另一个成员.Etcd运行在集群的每个coreos节点上,可以保证coreos集群的稳定,可靠的运行.当集群网络出现动荡 ...

  2. springboot2 redis

    本项目使用jar包: <parent> <groupId>org.springframework.boot</groupId> <artifactId> ...

  3. RFC2616-HTTP1.1-Methods(方法规定部分—译文)

    part of Hypertext Transfer Protocol -- HTTP/1.1RFC 2616 Fielding, et al. 9 方法定义 下面列出了有关HTTP/1.1协议的一些 ...

  4. shell && and ||

    2013-04-08 17:40:47   shell中&&和||的使用方法 &&运算符:   command1  && command2   & ...

  5. Win userAccountControl 基本属性

    userAccountControl 基本属性 属性标志 十六进制 十进制 说明 SCRIPT 0x0001 1 运行登录脚本 ACCOUNTDISABLE 0x0002 2 账户禁用 HOMEDIR ...

  6. Codeforces.472F.Design Tutorial: Change the Goal(构造 线性基 高斯消元)

    题目链接 \(Description\) 给定两个长为\(n\)的数组\(x_i,y_i\).每次你可以选定\(i,j\),令\(x_i=x_i\ \mathbb{xor}\ x_j\)(\(i,j\ ...

  7. JavaScript基础笔记(十四)最佳实践

    最佳实践 一)松散耦合 1.解耦HTML/JavaScript: 1)避免html种使用js 2)避免js种创建html 2.解耦CSS/JS 操作类 3.解耦应用逻辑和事件处理 以下是要牢记的应用和 ...

  8. php 创建返回结果配置文件 实例

    <?phpclass validateReturn{    //get return msg    function caseReturn($aRerurn)    {        $strM ...

  9. 潭州课堂25班:Ph201805201 django 项目 第二十五课 文章多级评论前后台实现 (课堂笔记)

    添加新闻评论功能 1.分析 业务处理流程: 判断前端传的新闻id是否为空,是否为整数.是否不存在 判断评论的内容是否为空 判断是否有父评论,父评论的id是否与新闻id匹配 判断用户是否登录 保存新闻评 ...

  10. hihoCoder 1143 : 骨牌覆盖问题·一(递推,矩阵快速幂)

    [题目链接]:click here~~ 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 骨牌,一种古老的玩具.今天我们要研究的是骨牌的覆盖问题: 我们有一个2xN的长条形 ...