EntityFramework 学习 一 CRUD using Stored Procedure: 使用存储过程进行CRUD操作
我们先创建如下3个存储过程
1.Sp_InsertStudentInfo:
CREATE PROCEDURE [dbo].[sp_InsertStudentInfo]
-- Add the parameters for the stored procedure here
@StandardId int = null,
@StudentName varchar(50)
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
2.sp_UpdateStudent:
CREATE PROCEDURE [dbo].[sp_UpdateStudent]
-- Add the parameters for the stored procedure here
@StudentId int,
@StandardId int = null,
@StudentName varchar(50)
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
3.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
将存储过程添加到EDM中
实体浏览器将存储过程添加到存储模型中,但是不引进函数
在EDM设计器中,右键Student实体,选择存储过程映射
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();
}
上面代码将执行如下存储过程
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
添加新实体,chontext上下文保存后,它将StudentId赋值,因为sp_InsertStudentInfo返回StudentId
EntityFramework 学习 一 CRUD using Stored Procedure: 使用存储过程进行CRUD操作的更多相关文章
- EntityFramework 学习 一 Stored Procedure
CREATE PROCEDURE [dbo].[GetCoursesByStudentId] -- Add the parameters for the stored procedure here @ ...
- Oracle Stored Procedure demo
1.how to find invalid status stored procedure and recompile them? SELECT OBJECT_NAME , status FROM u ...
- [转]How to get return values and output values from a stored procedure with EF Core?
本文转自:https://stackoverflow.com/questions/43935345/how-to-get-return-values-and-output-values-from-a- ...
- SQL Server 在多个数据库中创建同一个存储过程(Create Same Stored Procedure in All Databases)
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 遇到的问题(Problems) 实现代码(SQL Codes) 方法一:拼接SQL: 方法二: ...
- Stored Procedure 里的 WITH RECOMPILE 到底是干麻的?
在 SQL Server 创建或修改「存储过程(stored procedure)」时,可加上 WITH RECOMPILE 选项,但多数文档或书籍都写得语焉不详,或只解释为「每次执行此存储过程时,都 ...
- [转]Dynamic SQL & Stored Procedure Usage in T-SQL
转自:http://www.sqlusa.com/bestpractices/training/scripts/dynamicsql/ Dynamic SQL & Stored Procedu ...
- [原] XAF How to bind a stored procedure to a ListView in XAF
First, I suggest that you review the following topic to learn how to show a custom set of objects in ...
- Retrieving Out Params From a Stored Procedure With Python
http://www.rodneyoliver.com/blog/2013/08/08/retrieving-out-params-from-a-stored-procedure-with-pytho ...
- Modify a Stored Procedure using SQL Server Management Studio
In Object Explorer, connect to an instance of Database Engine and then expand that instance. Expand ...
随机推荐
- objective-C中的扩展方法与partial class
在c#中要扩展一个现有类非常easy,比方这样: ? 1 2 3 4 5 6 7 public static class Utils { public static void PrintTo ...
- 基于java spring框架开发部标1078视频监控平台精华文章索引
部标1078视频监控平台,是一个庞杂的工程,涵盖了多层协议,部标jt808,jt809,jt1078,苏标Adas协议等,多个平台功能标准,部标796标准,部标1077标准和苏标主动安全标准,视频方面 ...
- SpringBoot Idea 启动报错 Process finished with exit code 1
问题描述:没有其他任何错误日志,只有Process finished with exit code 1 问题原因:Maven POM.xml问题造成 由于是properties是我直接从其他项目中拷贝 ...
- 修改Linux字符集
1.查看操作系统中安装的locale信息 ls /usr/lib/locale 2.创建.i18n文件 在用户目录下创建.i18n文件,并添加如下内容: LANG="zh_CN.utf8&q ...
- 【Python+selenium】之奇怪问题总结
问题1: <_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'> Time Elapsed: 0:00:04 ...
- 在Linux中显示日历(cal)
cal 2013 显示2013年整年日历 cal 7 2013 显示2013年 7 月 日历
- [CTSC2001]1378 选课
1378 选课 题目描述 学校实行学分制.每门的必修课都有固定的学分,同时还必须获得相应的选修课程学分.学校开设了N(N<300)门的选修课程,每个学生可选课程的数量M是给定的.学生选修了这 ...
- HTML使用post方式提交中文内容出现乱码的错误解决方式
今天在做一个例子的时候,使用post方式提交表单,如果有中文的话,在另一个页面显示出来的时候,总是会出现乱码: 但是将提交方式改为get的时候,就不会出现这种错误. 详细错误见下面图片和代码. HTM ...
- elasticsearch从入门到出门-08-Elasticsearch容错机制:master选举,replica容错,数据恢复
假如: 9 shard,3 node Elasticsearch容错机制:master选举,replica容错,数据恢复 最佳分配情况: 这样分配之后,不管其中哪个node 宕机这个es 依然可以提供 ...
- swift 使用运行时进行属性关联
1.用OC思想写swift代码真得很爽,swift需要的OC基本上都有,只不过略微改变了一下,例如以前的Foundation库前缀NS全部去掉了,等等...思想其实都一样,不过swift确实非常精简, ...