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的更多相关文章

  1. Entity Framework Tutorial Basics(1):Introduction

    以下系列文章为Entity Framework Turial Basics系列 http://www.entityframeworktutorial.net/EntityFramework5/enti ...

  2. 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 ...

  3. Entity Framework Tutorial Basics(41):Multiple Diagrams

    Multiple Diagrams in Entity Framework 5.0 Visual Studio 2012 provides a facility to split the design ...

  4. 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 ...

  5. 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 ...

  6. Entity Framework Tutorial Basics(27):Update Entity Graph

    Update Entity Graph using DbContext: Updating an entity graph in disconnected scenario is a complex ...

  7. Entity Framework Tutorial Basics(22):Disconnected Entities

    Disconnected Entities: Before we see how to perform CRUD operation on disconnected entity graph, let ...

  8. Entity Framework Tutorial Basics(13):Database First

    Database First development with Entity Framework: We have seen this approach in Create Entity Data M ...

  9. 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 ...

随机推荐

  1. CodeForces - 156D:Clues(矩阵树定理&并查集)

    题意:给定N点,M边,求添加最少的边使之变为连通图的方案数. 思路:注意题目给出的M边可能带环,即最后生成的不一定是一棵树.但是影响不大.根据矩阵树定理,我们知道生成树的数量=N^(N-2),即点数^ ...

  2. 14.Selenium+Python使用火狐浏览器问题解决

    一开始使用的是IE浏览器作为自动化浏览器,但是由于想学习无头模式,故选择FireFox作为浏览器,以下是遇到的相关问题: 1.简单代码 from selenium import webdriver d ...

  3. laravel的小坑

    写控制器的名的时候只能出现一个首写大写字母, 后面的都为小写字母 否则会报找不到该控制器的错误

  4. MySQL mysqldump备份与恢复

    1 用户权限 grant select,RELOAD,PROCESS,SUPER, REPLICATION CLIENT ON *.* TO 'bak'@'192.168.%' IDENTIFIED ...

  5. rails登录后跳转到登录前的路径

    # 重定向到存储的地址或默认地址 def redirect_back_or(default) redirect_to(session[:forwarding_url] || default) sess ...

  6. SignalR推送服务在Android的实现 SignalA

    SignalA是老外写的用于实现.net端推送消息至安卓端的实现,支持版本为android 2.3或以上,由于我的版本最低是2.2,所以只有把源码下下来自己改,如果你觉得太多了可自己编译成jar引用, ...

  7. 实验吧CTF题库-安全杂项

    XDCTF misc200: 题目提示: 下载文件 用foremost分离文件 接下来用archpr 4.53进行明文攻击 先把00002700.zip中的readme.txt提取出来压缩成zip文件 ...

  8. 2015.1.3 DataGridView中嵌入其它控件

    1.按正常方法绑定待嵌入列的值,先赋值为空也行. 2.添加combbox到datagrivdview中 dvaw.Controls.Add(cb_dir); 3.添加DataGridView Mous ...

  9. 2015.3.12 C#运用正则表达式点滴

    Regex reg = new Regex(@"\w{1,}@\w{1,}\.(com)?(net)?"); //匹配(1个或多个)(字母或数字或下滑线)@ (1个或多个)(字母或 ...

  10. linux命令-su切换用户

    查看当前用户 #id uid=0(root) gid=0(root) 组=0(root) #whoami root ////////////////////////////////////////// ...