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 ...
随机推荐
- PHP数组排序和按数量分割
用PHP自带array_multisort函数排序 <?php $data = array(); $data[] = array('volume' => 67, 'editi ...
- BufferedInputStream与BufferedOutputStream
BufferedInputStream是带缓冲区的输入流,默认缓冲区大小是8M,能够减少访问磁盘的次数,提高文件读取性能:BufferedOutputStream是带缓冲区的输出流,能够提高文件的写入 ...
- Spring缓存源码剖析:(一)工具选择
从本篇开始对Spring 4.3.6版本中Cache部分做一次深度剖析.剖析过程中会对其中使用到的设计模式以及原则进行分析.相信对设计内功修炼必定大有好处. 一.环境及工具 IntelliJ IDEA ...
- windows提权辅助工具koadic
项目地址:https://github.com/zerosum0x0/koadic ┌─[root@sch01ar]─[/sch01ar] └──╼ #git clone https://github ...
- GNS3连接虚拟机
打开GNS3,拖一台路由器和主机 右击主机,选择配置 添加虚拟机的网卡为主机网卡,(选择VM1或VM8) 路由器选择虚拟机网卡连接 打开虚拟机在导航栏找到“虚拟机”-->“设 ...
- Java面向对象作业-用接口方式测试向下转型
Java面向对象作业-用接口方式测试向下转型 根据视频的里实例 我们直接修改Test2测试方法: package com.java1234.chap03.sec13; public class Tes ...
- leetcode144
/** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNo ...
- delphi 四舍五入
trunc取整 四舍五入 formatfloat('0.00', 2.1850) 看第二位,然后对后面的数字处理,偶数的话舍去,奇数四舍五入 System.Math.RoundTo(tempval,- ...
- 【知识结构】最强Web认证知识体系
花了些时间总结了下Web认证,以及各种方式的利弊和使用,后续后继续更新.文章转载请注明出处:https://www.cnblogs.com/pengdai/p/9144843.html -----20 ...
- 【279】◀▶ Python 运算符说明
参考:Python 运算符说明 目录: 一.算术运算符 二.比较(关系)运算符 三.赋值运算符 四.位运算符 五.逻辑运算符 六.成员运算符 七.身份运算符 八.运算符优先级 一.Python 算术运 ...