Entity Framework Tutorial Basics(29):Stored Procedure in Entity Framework
Stored Procedure in Entity Framework:
Entity Framework has the ability to automatically build native commands for the database based on your LINQ to Entities or Entity SQL queries, as well as, build the commands for inserting, updating, or deleting data. You may want to override these steps and use your own predefined stored procedures. You can use stored procedures either to get the data or to add/update/delete the records to one or multiple database tables.
Stored procedures and user-defined functions (UDFs) in the database are represented as functions in entity framework. EDM won't have any entity for the stored procedures in the EDM designer.
Here, we will add the following stored procedure GetCoursesByStudentId into EDM. This procedure returns all the courses assigned to a particular student:
CREATE PROCEDURE [dbo].[GetCoursesByStudentId]
-- Add the parameters for the stored procedure here
@StudentId int = null
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON; -- Insert statements for procedure here
select c.courseid, c.coursename,c.Location, c.TeacherId
from student s
left outer join studentcourse sc on sc.studentid = s.studentid
left outer join course c on c.courseid = sc.courseid
where s.studentid = @StudentId
END
First, create a new ADO.Net Entity Data Model using EF Designer from database.
Select GetCoursesByStudentId. Make sure that the Import selected stored procedures and functions into the entity model checkbox is selected and then click Finish.
You will see GetCoursesByStudentId added in Function Imports with new complex type GetCoursesByStudentId_Result in the Model Browser. Whenever you import a stored procedure into a model, it creates a new complex type with the name {sp name}_Result by default.
GetCoursesByStudentId returns the same fields defined in Course entity. So we don't need to add a new complex type for the returned data from GetCoursesByStudentId. You can change it by right clicking on GetCoursesByStudentId in function imports and selecting Edit. Check Entities and select Course from dropdown in popup window as shown below:
You will see the function in the context class for GetCoursesByStudentId as shown below:
Now, GetCoursesByStudentId can be called and the result shown below will be returned:
using (var context = new SchoolDBEntities())
{
var courses = context.GetCoursesByStudentId(); foreach (Course cs in courses)
Console.WriteLine(cs.CourseName);
}
The code shown above will execute the following statement:
exec [dbo].[GetCoursesByStudentId] @StudentId=1
You will learn how to use stored procedures for CUD operation in the next chapter.
Entity Framework Tutorial Basics(29):Stored Procedure in Entity Framework的更多相关文章
- Entity Framework Tutorial Basics(1):Introduction
以下系列文章为Entity Framework Turial Basics系列 http://www.entityframeworktutorial.net/EntityFramework5/enti ...
- Entity Framework Tutorial Basics(42):Colored Entity
Colored Entity in Entity Framework 5.0 You can change the color of an entity in the designer so that ...
- Entity Framework Tutorial Basics(41):Multiple Diagrams
Multiple Diagrams in Entity Framework 5.0 Visual Studio 2012 provides a facility to split the design ...
- Entity Framework Tutorial Basics(34):Table-Valued Function
Table-Valued Function in Entity Framework 5.0 Entity Framework 5.0 supports Table-valued functions o ...
- Entity Framework Tutorial Basics(32):Enum Support
Enum in Entity Framework: You can now have an Enum in Entity Framework 5.0 onwards. EF 5 should targ ...
- Entity Framework Tutorial Basics(27):Update Entity Graph
Update Entity Graph using DbContext: Updating an entity graph in disconnected scenario is a complex ...
- Entity Framework Tutorial Basics(22):Disconnected Entities
Disconnected Entities: Before we see how to perform CRUD operation on disconnected entity graph, let ...
- Entity Framework Tutorial Basics(13):Database First
Database First development with Entity Framework: We have seen this approach in Create Entity Data M ...
- Entity Framework Tutorial Basics(8):Types of Entity in Entity Framework
Types of Entity in Entity Framework: We created EDM for existing database in the previous section. A ...
随机推荐
- C# 如何将对象写入文件
http://wenku.baidu.com/link?url=QwDRlO1TeoubnmtUOitXXTRa-eZ6QFKvEuyXyzLXD9c0qCRUV5TL9Fq7_HqvxrMcwsAL ...
- BZOJ2784: [JLOI2012]时间流逝
BZOJ2784: [JLOI2012]时间流逝 https://lydsy.com/JudgeOnline/problem.php?id=2784 分析: 挺有意思的一道题. 注意到状态数是\(P( ...
- VC 6.0下载 VC 6.0英文版下载 Visual C++ 6.0 英文企业版 集成SP6完美版(最新更新地址,百度网盘)
下载地址1:Visual.C++.6.EN 下载地址2:Visual.C++.6.EN 更新下载地址可用(百度网盘)Visual.C++.6.EN 转载请注明出处,有技术问题,欢迎互相交流,或者留言.
- mysql之 percona-xtrabackup 2.4.7安装(热备工具)
准备:os是centos6.7,提前下载并上传 percona-xtrabackup 安装包,下载网址为: https://www.percona.com/downloads/XtraBackup/L ...
- think python chapter2
1.An assignment statement creates a new variable and gives it a value: Variable names can be as long ...
- laravel 添加自定义类 全局自定义方法 自定义常量
添加自定义类 https://blog.csdn.net/suchfool/article/details/38758367 https://blog.csdn.net/liukai6/article ...
- winrm service
今天看脚本忽然发现一个服务,叫winRM服务,这是个PowerShell的远程管理.开启它可以很大程度的方便用PowerShell操控! 下面是我找到的一些资料: 在Linux中,我们可以使用安全的S ...
- 阿里巴巴开源项目: canal 基于mysql数据库binlog的增量订阅&消费
背景 早期,阿里巴巴B2B公司因为存在杭州和美国双机房部署,存在跨机房同步的业务需求.不过早期的数据库同步业务,主要是基于trigger的方式获取增 量变更,不过从2010年开始,阿里系公司开始逐步的 ...
- Mina2中IoService
Apache MINA 2 是一个开发高性能和高可伸缩性网络应用程序的网络应用框架.它提供了一个抽象的事件驱动的异步 API,可以使用 TCP/IP.UDP/IP.串口和虚拟机内部的管道等传输方式. ...
- DevExpress TreeList GridView 样式设置
1.GridView 样式设置 this.gridViewUser.PaintStyleName = "Flat"; 2.TreeList 样式设置 this.treeListDe ...