Entity Framework Tutorial Basics(34):Table-Valued Function
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的更多相关文章
- Entity Framework Tutorial Basics(1):Introduction
以下系列文章为Entity Framework Turial Basics系列 http://www.entityframeworktutorial.net/EntityFramework5/enti ...
- 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 ...
- Entity Framework Tutorial Basics(43):Download Sample Project
Download Sample Project: Download sample project for basic Entity Framework tutorials. Sample projec ...
- 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(37):Lazy Loading
Lazy Loading: One of the important functions of Entity Framework is lazy loading. Lazy loading means ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 细说移动前端Android联调
为什么要联调 A:正在疯狂coding的时候,产品MM过来,焦急的说两周前的一个页面在手机上显示略微错位,但小本上显示正常! B:本着爱折腾的原则,作为大前端,在移动互联网时代的基本技能. 联调的方式 ...
- Maven:Resource Path Location Type Project configuration is not up-to-date with pom.xml. Run project configuration update
Maven构建项目的时候提示: Description Resource Path Location Type Project configuration is not up-to-date with ...
- 开始写博客,与ITer们互相学习
学习计算机也6年了,一直待在学校里.这些年来很多学习资料都来自网络上的技术博客,非常感谢各位 ITer 的分享精神,鄙人从中受益匪浅.从今天起也挤出时间开始写技术博客.主要是把自己这些年的一些技术文档 ...
- HDU2841(容斥原理)
Visible Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- Java学习之系统高可用性渲染接口日志自动服务降级
背景:公司都追求系统的高可用性,这里不可用时间就是其中很重要的一个指标,为此在做系统功能升级迭代的过程中如何快速处理异常恢复正常功能极为重要.现在对新增模块的要求是都增加开关,方便快速关闭异常模块,但 ...
- mysql查询最近30天、7天、每天、昨天、上个月的记录
一些变量说明: add_time为插入的时间 to_days是sql函数,返回的是个天数 data_sub(date,INTERVAL expr type)给指定的日期减去多少天 data()函数 ...
- Python类(三)-多继承的区别
多继承的有两个方式,一个是广度优先,一个是深度优先Python2中经典类按深度优先,新式类按广度优先Python3中经典类和新式类都按广度优先 # -*- coding:utf-8 -*- __aut ...
- apache将不带www域名301重定向到带www的域名的配置方法
#强制重定向到wwwRewriteEngine OnRewriteCond %{HTTP_HOST} ^jb51.net/ [NC]RewriteRule ^(.*)$ http://www.jb51 ...
- java反射专题三
一丶调用运行时类中指定的属性 Class clazz = Person.class; //1.获取指定的属性 Field name = clazz.getField("name") ...
- 10-08C#基础--进制转换
(一).数制 计算机中采用的是二进制,因为二进制具有运算简单,易实现且可靠,为逻辑设计提供了有利的途径.节省设备等优点,为了便于描述,又常用八.十六进制作为二进制的缩写.一般计数都采用进位计数,其特点 ...