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. len(),range()函数

    len()函数返回字符串.列表.字典.元组等长度 eg1:计算字符串的长度: >>>s='hello good boy doiido' >>>len(s) > ...

  2. bgcolor RGB 和16进制之间的转换,16进制转RGB,源码

    <p>bgcolor RGB 和16进制之间的转换,16进制转RGB,源码例如:<br /> 输入 201,255,201 转换成 #C9FFC9</p> < ...

  3. (转)Linux sort命令

    Linux 的 ‘sort’命令的14个有用的范例(一) 2015-5-2 10:29    评论: 3 收藏: 10 编译自:http://www.tecmint.com/sort-command- ...

  4. 《Javascript高级程序设计》阅读记录(二):第四章

    这个系列之前文字地址:http://www.cnblogs.com/qixinbo/p/6984374.html 这个系列,我会把阅读<Javascript高级程序设计>之后,感觉讲的比较 ...

  5. word 2007,以不同颜色突出显示文本的快捷键,highlight命令

    命令:highlight 默认快捷键:Ctrl+Alt+H   查询或自定义快捷键的方法: 打开一个文档→单击左上角的office图标→word选项 左边的列表中选择自定义→在右边的窗口中,底部有个“ ...

  6. touch: cannot touch '/usr/local/tomcat/logs/catalina.out': Permission denied解决方法

    一.报以下错误: ./startup.sh Using CATALINA_BASE: /usr/local/tomcat702 Using CATALINA_HOME: /usr/local/tomc ...

  7. Mina APR传输与串口传输讲解---zhengli

    原文地址:Mina Transports(Apache Mina user guide Chapter6 Transport) 1.1.1.APR传输 1.1.1.1.介绍 APR (Apache P ...

  8. Rails的静态资源管理(二)—— 如何使用 Asset Pipeline

    官方文档:http://guides.ruby-china.org/asset_pipeline.html http://guides.rubyonrails.org/asset_pipeline.h ...

  9. sqlplus 设置显示格式

    使用sqlplus查询显示结果,显示很乱,下面有种方法可以让她显示的更好看些.1.设置显示的宽度:设置前可以先查看当前宽度: SQL> show linesize;linesize 100SQL ...

  10. Navicat断网时连不上数据库

    最近安装了破解的Navicat,在有网的条件下可以连接本地安装的MySQL数据库,但断网之后就不可以,如下: 于是上网查资料,发现原因为: localhost可以看成是一个域名,在一大部分情况下,它能 ...