博客
关于我
16.翻译系列:EF 6 Code -First中使用存储过程【EF 6 Code-First系列】
阅读量:411 次
发布时间:2019-03-06

本文共 2495 字,大约阅读时间需要 8 分钟。

原文链接:

 

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
() .MapToStoredProcedures(); } public DbSet
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]ASBEGIN    INSERT [dbo].[Students]([StudentName], [DoB])    VALUES (@StudentName, @DoB)        DECLARE @StudentId int    SELECT @StudentId = [StudentId]    FROM [dbo].[Students]    WHERE @@ROWCOUNT > 0 AND [StudentId] = scope_identity()        SELECT t0.[StudentId]    FROM [dbo].[Students] AS t0    WHERE @@ROWCOUNT > 0 AND t0.[StudentId] = @StudentIdENDCREATE PROCEDURE [dbo].[Student_Update]    @StudentId [int],    @StudentName [nvarchar](max),    @DoB [datetime]ASBEGIN    UPDATE [dbo].[Students]    SET [StudentName] = @StudentName, [DoB] = @DoB    WHERE ([StudentId] = @StudentId)ENDCREATE PROCEDURE [dbo].[Student_Delete]    @StudentId [int]ASBEGIN    DELETE [dbo].[Students]    WHERE ([StudentId] = @StudentId)END

 

为实体映射自定义的存储过程

 

EF6允许你使用自己的存储过程,你可以像下面这样进行配置,下面的代码为Student实体,映射了一个自定义的存储过程。

protected override void OnModelCreating(DbModelBuilder modelBuilder){    modelBuilder.Entity
() .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存储过程。仅仅是映射其中一个,是不被允许的。

 

转载地址:http://zvxkz.baihongyu.com/

你可能感兴趣的文章
NLP:从头开始的文本矢量化方法
查看>>
NLP:使用 SciKit Learn 的文本矢量化方法
查看>>
nmap 使用方法详细介绍
查看>>
nmap使用
查看>>
Nmap扫描教程之Nmap基础知识
查看>>
nmap指纹识别要点以及又快又准之方法
查看>>
Nmap渗透测试指南之指纹识别与探测、伺机而动
查看>>
Nmap端口扫描工具Windows安装和命令大全(非常详细)零基础入门到精通,收藏这篇就够了
查看>>
NMAP网络扫描工具的安装与使用
查看>>
NMF(非负矩阵分解)
查看>>
nmon_x86_64_centos7工具如何使用
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>