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. linux下的第一个C程序及其编译方法

    #include <stdio.h> #include <stdlib.h>   int main(int argc, char ** argv) {     printf(& ...

  2. Docker registry 与 持续集成

    1.如何建立私有的 Docker Hub docker是一个非常好用的虚拟化工具. Registry 在git上分为老代码库和新代码库,老代码push,pull 存在性能问题,新代码库采用go语言编写 ...

  3. django的related_name

    转:https://segmentfault.com/q/1010000003705677 就是一个反向关联的属性,比方说model里面定义两个class,一个是A,一个是B class A(Mode ...

  4. mezzanine安装配置

    ubuntu 14.04 cd /var/wwwmkdir testingcd testingvirtualenv venv --python=python3. venv/bin/activate p ...

  5. 容器中跨主机的网络方案-flannel

    容器中的网络是建立docker集群的重要内容. 本文将介绍如何用flannel实现容器的多节点互通. 下图是flannel的实现原理,摘自: http://docker-k8s-lab.readthe ...

  6. 如何隐藏掉Nginx的版本号

    最近新学习了一个命令curl,里面有一个参数-I可以查看到网站使用的是哪种服务器,比如: zhangxiaoliudeMacBook-Pro-2:~ zhangxiaoliu$ curl -I htt ...

  7. hihoCoder#1079(线段树+坐标离散化)

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho在回国之后,重新过起了朝7晚5的学生生活,当然了,他们还是在一直学习着各种算法~ 这天小Hi和小Ho所在的学 ...

  8. 一些js和JQuery的方法

    取值填值: js: get-- innerText, innerHTML, value;  set-- innerText=?, innerHTML=?, value=?; JQuery: get-- ...

  9. POJ 1276 Cash Machine(单调队列优化多重背包)

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 38986   Accepted: 14186 De ...

  10. 分治思想的应用:C++实现快速排序和随机化的快速排序

    分治思想的应用:C++实现快速排序和随机化的快速排序 原创 2014年09月08日 14:04:49 标签: 快速排序 / 随机化快速排序 / 排序算法 / 数据结构 947 1. 快速排序时冒泡排序 ...