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 ...
随机推荐
- UVA12296 Pieces and Discs
题意 PDF 分析 可以看成直线切割多边形,直接维护. 对每个多边形考虑每条边和每个点即可. 时间复杂度?不过\(n,m \leq 20\)这种数据怎么都过了.据说是\(O(n^3)\)的,而且常数也 ...
- Java面试题:如何对HashMap按键值排序
Java中HashMap是一种用于存储“键”和“值”信息对的数据结构.不同于Array.ArrayList和LinkedLists,它不会维持插入元素的顺序. 因此,在键或值的基础上排序HashMap ...
- Python学习笔记之selenium 定制启动 chrome 的选项
在自动化中,默认情况下我们打开的就是一个普通的纯净的chrome浏览器,而我们平时在使用浏览器时,经常就添加一些插件,扩展,代理之类的应用.所以使用 selenium 时,我们可能需要对 chrome ...
- 常用JavaScript操作页面元素的方法
1.取得dropdownlist的选中值 var ddl =document.getElementById('<%=ddlusers.ClientID%>'); var index = d ...
- svn配置及基本使用
svn软件下载地址http://subversion.apache.org/packages.html在安装TortoiseSVN时安装客户端和服务端 下以svn在windows下使用为例,linux ...
- casperjs 安装试用
纠结了好久,一直报错, 具体错误信息现在已经忘了,后来看了官网的prerequisite,才知道要安装特定版本或greater 的 phantomjs 和 Python.我这边主要是没装python. ...
- laravel安装初体验
1.github下载laravel 2.通过composer安装相应的库 composer config repo.packagist composer https://packagist.phpco ...
- QQ市场总监分享:黏住90后的独门攻略
转自:http://www.gameres.com/476003.html 90后的关键词 1. 品质生活 90后是怎么样的一群人?他们注重生活的品质. 他们比我们更爱享受,或者说他们不像我们一样认为 ...
- Python——List
一.集成开发环境 集成开发环境(IDE,Integrated development Enviroment)是用于提供程序开发环境的应用程序,一般包括代码编辑器.编译器.调试器和图形用户界面等工具.集 ...
- re模块练习
1.匹配标签 1 import re 2 ret = re.search('<(?P<tag_name>\w+)>\w+</(?P=tag_name)>','< ...