CRUD using Stored Procedure:

In the previous chapter, we have seen how to get data using a stored procedure. In this chapter, we will use stored procedures for CUD (create, update, delete) operation using DbContext. That means context will execute stored procedures instead of DDL statements on context.SaveChanges().

We will use the following stored procedures:

  1. sp_InsertStudentInfo stored procedure to insert a new student into the database
  2. sp_UpdateStudent to update the student
  3. sp_DeleteStudent to delete the student in the database.

Sp_InsertStudentInfo:

CREATE PROCEDURE [dbo].[sp_InsertStudentInfo]
-- Add the parameters for the stored procedure here
@StandardId int = null,
@StudentName varchar
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON; INSERT INTO [SchoolDB].[dbo].[Student]([StudentName],[StandardId])
VALUES(@StudentName, @StandardId) SELECT SCOPE_IDENTITY() AS StudentId END

sp_UpdateStudent:

CREATE PROCEDURE [dbo].[sp_UpdateStudent]
-- Add the parameters for the stored procedure here
@StudentId int,
@StandardId int = null,
@StudentName varchar
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON; Update [SchoolDB].[dbo].[Student]
set StudentName = @StudentName,StandardId = @StandardId
where StudentID = @StudentId; END

sp_DeleteStudent

CREATE PROCEDURE [dbo].[sp_DeleteStudent]
-- Add the parameters for the stored procedure here
@StudentId int
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON; DELETE FROM [dbo].[Student]
where StudentID = @StudentId END

First of all, add these stored procedures into EDM and make sure that the Import selected stored procedures and function into the entity model checkbox is unchecked as we will map these procedures with Student entity directly.

Now, Model Browser will add procedures into Storage model but not in Function Imports

In the EDM designer, right click on Student entity and select Stored Procedure Mapping to open Mapping details:

In the Mapping Details, you will see <Select Insert Function>, <Select Update Function>, and <Select Delete Function>. Select the appropriate stored procedure for each one, e.g. Select sp_InsertStudentInfo for Insert function, as shown below:

sp_InsertStudentInfo returns new auto generated StudentId. Map that with Student Entity’s StudentID as shown below:

Complete the mapping of Insert, Update and Delete procedures as shown below:

Now, we need to validate it before executing to ensure that there will not be a run time error. To accomplish this, right click on Student entity in the designer and click Validate and make sure that there are no warnings or errors:

Now you can add, update, and delete student as shown below:

using (var context = new SchoolDBEntities())
{
Student newStudent = new Student() { StudentName = "New Student using SP"}; context.Students.Add(newStudent);
//will execute sp_InsertStudentInfo
context.SaveChanges(); newStudent.StudentName = "Edited student using SP";
//will execute sp_UpdateStudent
context.SaveChanges(); context.Students.Remove(newStudent);
//will execute sp_DeleteStudentInfo
context.SaveChanges();
}

The code shown above will execute the following stored procedures on each SaveChanges():

exec [dbo].[sp_InsertStudentInfo] @StandardId=NULL,@StudentName='New Student using SP'
go exec [dbo].[sp_UpdateStudent] @StudentId=47,@StandardId=NULL,@StudentName='Edited student using SP'
go exec [dbo].[sp_DeleteStudent] @StudentId=47
go

Note: Once context calls SaveChanges after adding a new student, it will assign new StudentID to StudentID property of the Student entity because sp_InsertStudentInfo returns StudentId. This is necessary in order to use that entity object for further operation.

Download sample project for the demo.

最新文章

  1. ABP配套代码生成器(ABP Code Generator)帮助文档,实现快速开发
  2. Unity自动寻路Navmesh之高级
  3. C++ 中指针与引用的区别
  4. SQL Server技术问题之触发器优缺点
  5. 一道c语言运算符优先级问题
  6. 暴力枚举-数长方形(hdu5258)
  7. linux_sed 正则替换
  8. css——样式表分类,选择器
  9. [Educational Round 10][Codeforces 652F. Ants on a Circle]
  10. UVA11093-Just Finish it up(思维)
  11. iOS scrollsToTop属性失效
  12. 小而美的ghost driver
  13. java 错误: 找不到或无法加载主类
  14. 织梦调用多个栏目typeid="1,2,3"不支持的解决方法
  15. Linux基础知识之history的详细说明
  16. 转---Post/Redirect/Get pattern
  17. linux中的redis缓存服务器
  18. Java数据封装类
  19. 「小程序JAVA实战」小程序通用模板的使用(17)
  20. CNI:容器网络接口

热门文章

  1. python 类的定义和继承
  2. Arc066_F Contest with Drinks Hard
  3. C#检查网络是否可以连接互联网
  4. BZOJ2038:[2009国家集训队]小Z的袜子
  5. 聊聊“现在学习MFC有用吗?”
  6. IPv4 forwarding is disabled. Networking will not work.
  7. 关于HTML标签中的一些容易忘记常用样式属性
  8. java继承初级
  9. Ubuntu14.04LTS上安装Pip
  10. Celery-4.1 用户指南: Canvas: Designing Work-flows(设计工作流程)