Entity Framework 6.0 Tutorials(9):Stored Procedure Mapping
Code First - Insert, Update, Delete Stored Procedure Mapping:
Entity Framework 6 Code-First provides the ability to create and use a stored procedure for add, update, and delete operations. This was not possible in the previous versions of Entity Framework.
Student Entity:
class Student
{
public Student()
{
} public int Student_ID { get; set; }
public string StudentName { get; set; }
}
The following example automatically creates a stored procedure for Student entity using Fluent API.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Student>()
.MapToStoredProcedures();
}
The code shown above will create three procedures Student_Insert, Student_Update and Student_Delete. Student_Insert and Student_Update stored procedures have a parameter name which corresponds to the property names. Student_Delete will have a primary key property StudentID parameter.
You can also change the stored procedure and parameter names, as shown below:
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.Student_ID, "Student_ID"))
.Update(sp => sp.HasName("sp_UpdateStudent").Parameter(pm => pm.StudentName, "name"))
.Delete(sp => sp.HasName("sp_DeleteStudent").Parameter(pm => pm.Student_ID, "Id"))
);
}
If you want all your entities to use stored procedures, then do the following:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Types().Configure(t => t.MapToStoredProcedures());
}
Limitations:
- Only Fluent API can be used to map stored procedures. You cannot use Data Annotation attributes in EF 6 for stored procedure mapping.
- You cannot use a mixture of stored procedure and query to add, update and delete operation on the same entity. You can either use stored procedure or SQL query for all add, update and delete operations with the entity.
Download sample project for demo.
Visit codeplex documentation for more information.
Entity Framework 6.0 Tutorials(9):Stored Procedure Mapping的更多相关文章
- Entity Framework 6.0 Tutorials(1):Introduction
以下系统文章为EF6.0知识的介绍,本章是第一篇 原文地址:http://www.entityframeworktutorial.net/entityframework6/introduction.a ...
- Entity Framework 6.0 Tutorials(4):Database Command Logging
Database Command Logging: In this section, you will learn how to log commands & queries sent to ...
- Entity Framework 6.0 Tutorials(11):Download Sample Project
Download Sample Project: Download a sample project for Entity Framework 6 Database-First model below ...
- Entity Framework 6.0 Tutorials(10):Index Attribute
Index Attribute: Entity Framework 6 provides Index attribute to create Index on a particular column ...
- Entity Framework 6.0 Tutorials(6):Transaction support
Transaction support: Entity Framework by default wraps Insert, Update or Delete operation in a trans ...
- Entity Framework 6.0 Tutorials(3):Code-based Configuration
Code-based Configuration: Entity Framework 6 has introduced code based configuration. Now, you can c ...
- Entity Framework 6.0 Tutorials(2):Async query and Save
Async query and Save: You can take advantage of asynchronous execution of .Net 4.5 with Entity Frame ...
- Entity Framework 6.0 Tutorials(8):Custom Code-First Conventions
Custom Code-First Conventions: Code-First has a set of default behaviors for the models that are ref ...
- Entity Framework 6.0 Tutorials(7):DbSet.AddRange & DbSet.RemoveRange
DbSet.AddRange & DbSet.RemoveRange: DbSet in EF 6 has introduced new methods AddRange & Remo ...
随机推荐
- Java开发过程中乱码问题理解
1.Java编译器(即编译成class文件时) 用的是unicode字符集.2.乱码主要是由于不同的字符集相互转换导致的,理论上各个字符的编码规则是不同的,是不能相互转换的,所以根本解决乱码的方法就是 ...
- subline自定义快捷键
由于感觉原始subline的运行快捷键ctrl+b不是很方便,尝试修改,方法如下: 打开首选项 --> 快捷键设置 Key Bindings -Default //这个表示系统默认的快捷键.Ke ...
- photoshop画矩形款
1.屏幕截图 2.在photoshop新建图形 3.用矩形选框 4.右键打开描边,宽度设成5个像素
- 支付宝sdk集成
支付宝开放平台 http://doc.open.alipay.com/doc2/detail?treeId=59&articleId=103563&docType=1 集成步骤: 1. ...
- erlang热部署
以下流程参考rebar的wiki,亲测 rebar的版本一定要注意,高版本对于下面两个指令有bug rebar generate-appups rebar generate-upgrade 经过一个个 ...
- unittest框架+ HTMLTestRunner 出报告时,展示控制台信息 不同展示的参数写法 加verbosity
加verbosity参数 没有加的时候展示: 参考: 来源: https://www.cnblogs.com/tomweng/p/6609937.html 介绍: HTMLTestRunner 是 ...
- java图形化界面-------鼠标监听画圆----------使用匿名类
package com.aa; import java.awt.Color; import java.awt.Graphics; import java.awt.event.MouseAdapter; ...
- java练习,,,从键盘输入次数,输出最大值,和
总结:你的关注,是我的动力 package com.b; import java.util.Scanner; public class YUIO { public static void main(S ...
- Java-Maven-Runoob:Maven 构建 & 项目测试
ylbtech-Java-Maven-Runoob:Maven 构建 & 项目测试 1.返回顶部 1. Maven 构建 & 项目测试 在上一章节中我们学会了如何使用 Maven 创建 ...
- python学习(五) 条件、循环和其他语句
第五章 条件.循环和其他语句 5.1 print和import的更多信息 5.1.1 使用逗号输出 >>> print('age',43,45) // 可以用逗号隔开 ...