Table-Valued Function in Entity Framework 5.0

Entity Framework 5.0 supports Table-valued functions of SQL Server.

Table-valued functions are similar to stored procedure with one key difference: the result of TVF is composable which means that it can be used in a LINQ query.

We have created a TVF GetCourseListByStudentID in the database that will return all the courses of a particular student. For example:

USE [SchoolDB]
GO
/****** Object: UserDefinedFunction [dbo].[GetCourseListByStudentID] */
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[GetCourseListByStudentID]
(
-- Add the parameters for the function here
@studentID int
)
RETURNS TABLE
AS
RETURN
(
-- Add the SELECT statement with parameter references 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
)

Now, update your EDM and add this TVF into your EDM. Right click on the designer → select Update Model from the Database..

Expand Stored Procedures and Functions node → expand schema node (dbo schema in our case) → select 'GetCourseListByStudentID' and click Finish. Make sure that the checkbox for 'Import selected procedures and functions into the entity model' is checked (this will import the function automatically).

After you imported the function, you can verify it: Open Model Browser → expand Function Imports → right click on imported function 'GetCourseListByStudentID' → click Edit:

You can see that EDM has automatically created the complex type GetCourseListByStudentID_Result as a return collection type.

You can also select an existing entity as a return type if TVF returns the same columns as entity:

Now, you can use TVF with DBContext. For example:

using (var ctx = new SchoolDBEntities())
{
//Execute TVF and filter result
var courseList = ctx.GetCourseListByStudentID().Where(c => c.Location.SpatialEquals(DbGeography.FromText("POINT(-122.360 47.656)"))))
.ToList<GetCourseListByStudentID_Result>(); foreach (GetCourseListByStudentID_Result cs in courseList)
Console.WriteLine("Course Name: {0}, Course Location: {1}",
cs.CourseName, cs.Location);
}

Entity Framework Tutorial Basics(34):Table-Valued Function的更多相关文章

  1. Entity Framework Tutorial Basics(1):Introduction

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

  2. Entity Framework Tutorial Basics(4):Setup Entity Framework Environment

    Setup Entity Framework Environment: Entity Framework 5.0 API was distributed in two places, in NuGet ...

  3. Entity Framework Tutorial Basics(43):Download Sample Project

    Download Sample Project: Download sample project for basic Entity Framework tutorials. Sample projec ...

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

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

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

  6. Entity Framework Tutorial Basics(37):Lazy Loading

    Lazy Loading: One of the important functions of Entity Framework is lazy loading. Lazy loading means ...

  7. Entity Framework Tutorial Basics(36):Eager Loading

    Eager Loading: Eager loading is the process whereby a query for one type of entity also loads relate ...

  8. Entity Framework Tutorial Basics(33):Spatial Data type support in Entity Framework 5.0

    Spatial Data type support in Entity Framework 5.0 MS SQL Server 2008 introduced two spatial data typ ...

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

随机推荐

  1. 掌握sudo的使用

    “sudo”是Unix/Linux平台上的一个非常有用的工具,它允许系统管理员分配给普通用户一些合理的“权利”,让他们执行一些只有超级用户或其他 特许用户才能完成的任务,比如:运行一些像mount,h ...

  2. 网页播放rtsp流媒体

    解决方法: 1.搭建Red5流媒体服务器(其他还有CRTMPD.NGINX-RTMP.SRS) 2.FFmpeg(其他还有live555)实现监控摄像头的RTSP协议转RTMP协议直播 3.搭建WEB ...

  3. UVA - 11768 Lattice Point or Not (扩展欧几里得)

    求一条线段上有多少个整点. 是道扩欧基础题,列出两点式方程,然后分四种情况讨论即可.但细节处理较多很容易写挫(某zzWA了十几发才过掉的). 由于数据精度较小,浮点数比较没有用eps,直接==比较了. ...

  4. 自编jQuery插件实现模拟alert和confirm

    现在绝大多数网站都不用自带的alert和confirm了,因为界面太生硬了.因此这个插件就这样产生了自己定制一个的想法...... 啥也不说,先上图,有图有真相 :) 现在绝大多数网站都不用自带的al ...

  5. Keepalived+Nginx实现负载均衡高可用

    一.负载均衡高可用 Nginx作为负载均衡器,所有请求都到了Nginx,可见Nginx处于非常重点的位置,如果Nginx服务器宕机后端web服务将无法提供服务,影响严重. 为了避免负载均衡服务器的宕机 ...

  6. boot asio 非阻塞同步编程---非阻塞的accept和receive。

    boot asio 非阻塞同步编程---非阻塞的accept和receive. 客户端编程: #include<boost/timer.hpp> #include <iostream ...

  7. Swing编程中为什么对Panel设置了位置后,它的lable位置也跟这变了呢?

    总结: package com.da; //这里我不明白,我对每一个Panle里的按钮设置了位置BorderLayout.SOUTH.可是为什么Lable的位置和它一样同步设置了呢? //做一个界面 ...

  8. 2016第十三届浙江省赛 D - The Lucky Week

    D - The Lucky Week Edward, the headmaster of the Marjar University, is very busy every day and alway ...

  9. Java-API-Package:javax.annotation

    ylbtech-Java-API-Package:javax.annotation 1.返回顶部 1. Package javax.annotation Enum Summary Resource.A ...

  10. java 多线程系列---JUC原子类(三)之AtomicLongArray原子类

    AtomicLongArray介绍和函数列表 在"Java多线程系列--“JUC原子类”02之 AtomicLong原子类"中介绍过,AtomicLong是作用是对长整形进行原子操 ...