16.翻译系列:EF 6 Code -First中使用存储过程【EF 6 Code-First系列】
EF 6 Code-First系列文章目录:
- 1 翻译系列:什么是Code First(EF 6 Code First 系列)
- 2.翻译系列:为EF Code-First设置开发环境(EF 6 Code-First系列)
- 3.翻译系列:EF Code-First 示例(EF 6 Code-First系列)
- 4.翻译系列:EF 6 Code-First默认约定(EF 6 Code-First系列)
- 5.翻译系列:EF 6中数据库的初始化(EF 6 Code-First 系列)
- 6.翻译系列:EF 6 Code-First中数据库初始化策略(EF 6 Code-First系列
- 7.翻译系列:EF 6中的继承策略(EF 6 Code-First 系列)
- 8.翻译系列: EF 6中配置领域类(EF 6 Code-First 系列)
- 9.翻译系列:EF 6以及EF Core中的数据注解特性(EF 6 Code-First系列)
- 9.1 翻译系列:数据注解特性之----Table【EF 6 Code-First 系列】
- 9.2 翻译系列:数据注解特性之---Column【EF 6 Code First系列】
- 9.3 翻译系列:数据注解特性之Key【EF 6 Code-First 系列】
- 9.4 翻译系列:EF 6以及 EF Core中的NotMapped特性(EF 6 Code-First系列)
- 9.5 翻译系列:数据注解之ForeignKey特性【EF 6 Code-First系列】
- 9.6 翻译系列:数据注解之Index特性【EF 6 Code-First系列】
- 9.7 翻译系列:EF数据注解特性之--InverseProperty【EF 6 Code-First系列】
- 9.8 翻译系列:数据注解特性之--Required 【EF 6 Code-First系列】
- 9.9 翻译系列:数据注解特性之--MaxLength 【EF 6 Code-First系列】
- 9.10 翻译系列:EF数据注解特性之StringLength【EF 6 Code-First系列】
- 9.11 翻译系列:数据注解特性之--Timestamp【EF 6 Code-First系列】
- 9.12 翻译系列:数据注解特性之ConcurrencyCheck【EF 6 Code-First系列】
- 10.翻译系列:EF 6中的Fluent API配置【EF 6 Code-First系列】
- 10.1.翻译系列:EF 6中的实体映射【EF 6 Code-First系列】
- 10.2.翻译系列:使用Fluent API进行属性映射【EF 6 Code-First】
- 11.翻译系列:在EF 6中配置一对零或者一对一的关系【EF 6 Code-First系列】
- 12.翻译系列:EF 6 中配置一对多的关系【EF 6 Code-First系列】
- 13.翻译系列:Code-First方式配置多对多关系【EF 6 Code-First系列】
- 14.翻译系列:从已经存在的数据库中生成上下文类和实体类【EF 6 Code-First系列】
- 15.翻译系列:EF 6中的级联删除【EF 6 Code-First 系列】
- 16.翻译系列:EF 6 Code -First中使用存储过程【EF 6 Code-First系列】
- 17.翻译系列:将Fluent API的配置迁移到单独的类中【EF 6 Code-First系列】
- 18.翻译系列:EF 6 Code-First 中的Seed Data(种子数据或原始测试数据)【EF 6 Code-First系列】
- 19.翻译系列:EF 6中定义自定义的约定【EF 6 Code-First约定】
- 20.翻译系列:Code-First中的数据库迁移技术【EF 6 Code-First系列】
- 20.1翻译系列:EF 6中自动数据迁移技术【EF 6 Code-First系列】
- 20.2.翻译系列:EF 6中基于代码的数据库迁移技术【EF 6 Code-First系列】
- 21.翻译系列:Entity Framework 6 Power Tools【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_Insert, Student_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系列】的更多相关文章
- 9.2 翻译系列:数据注解特性之---Column【EF 6 Code First系列】
原文链接:http://www.entityframeworktutorial.net/code-first/column-dataannotations-attribute-in-code-firs ...
- 21.翻译系列:Entity Framework 6 Power Tools【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/entity-framework-power-tools.aspx 大家好,这里就是EF ...
- 9.10 翻译系列:EF数据注解特性之StringLength【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/stringlength-dataannotations-attribute-in-co ...
- 9.9 翻译系列:数据注解特性之--MaxLength 【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/maxlength-minlength-dataannotations-attribut ...
- 9.7 翻译系列:EF数据注解特性之--InverseProperty【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/inverseproperty-dataannotations-attribute-in ...
- 9.3 翻译系列:数据注解特性之Key【EF 6 Code-First 系列】
原文链接:http://www.entityframeworktutorial.net/code-first/key-dataannotations-attribute-in-code-first.a ...
- 9.1 翻译系列:数据注解特性之----Table【EF 6 Code-First 系列】
原文地址:http://www.entityframeworktutorial.net/code-first/table-dataannotations-attribute-in-code-first ...
- 9.8 翻译系列:数据注解特性之--Required 【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/required-attribute-dataannotations-in-code-f ...
- 9.11 翻译系列:数据注解特性之--Timestamp【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/TimeStamp-dataannotations-attribute-in-code- ...
随机推荐
- UVa 679 - Dropping Balls【二叉树】【思维题】
题目链接 题目大意: 小球从一棵所有叶子深度相同的二叉树的顶点开始向下落,树开始所有节点都为0.若小球落到节点为0的则往左落,否则向右落.并且小球会改变它经过的节点,0变1,1变0.给定树的深度D和球 ...
- Rendering React components to the document body
React一个比较好用的功能是其简单的API,一个组件可以简单到一个return了组件结构的render函数.除了一个简单的函数之外,我们还有了一段有用且可复用的代码片段. 问题 不过有时候可能会受到 ...
- Farewell Party-构造
Farewell Party 思路 : 转换思路 ,有 a [ i ] 个不相等的 ,那么至少得有 n - a [ i ]个与它相等的. 但是有可能与它拥有相同数目的有很多. 但是为了能够最终 分配成 ...
- 《重回耶路撒冷——犹太人的三千年》(Return to Jerusalem)读后感
写在前面 书名:<重回耶路撒冷——犹太人的三千年>(Return to Jerusalem) 作者:张力升 来源:长清图书馆 阅读用时:其实年前拿到书,本来想寒假在家看的,但是在家一点儿都 ...
- Python3基础系列-程序模板及代码本质
概要 横看成岭侧成峰,远近高低各不同.但是,程序的设计核心思想却是很简单,简单理解就是有一个输入,对输入的处理环节,最后得出一个输出.这个过程中的设计及其实现却是各不相同.本节的主要内容如下: 程序设 ...
- Spring Boot基础讲解
Spring Boot Spring Boot 是由Pivotal团队提供的框架,它并不是一个全新的框架,而是将已有的 Spring 组件整合起来,设计目的是用来简化新Spring应用的初始搭建以及开 ...
- 【DWM1000】 code 解密5一ACHOR 第一次回家Main 函数
instance_run(); if((instance_data[0].monitor == 1) && ((portGetTickCnt() - instance_data[0]. ...
- NOIP考纲总结+NOIP考前经验谈
首先来一张图,很直观(截止到2012年数据) 下面是收集的一些,我改了一下 红色加粗表示特别重要,必须掌握 绿色加粗表示最好掌握,可能性不是很大,但是某些可以提高程序效率 高精度 a.加法 b.减法 ...
- 逻辑运算的妙用-Single Number
题目:一个int的array,除了一个元素只有一个其余的都是两个,找到这一个的元素. 使用:逻辑运算 XOR异或运算 关于逻辑运算的总结[转] &&和||:逻辑运算符 &和|: ...
- android Resources 类的使用
使用 R.<resource_type>.<resource_name> 获取的是资源的一个 id (int 类型), 但有时候我们需要获取资源本身,这时候我们可以通过 Res ...