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 ... 
随机推荐
- BZOJ - 2588 Spoj 10628. Count on a tree (可持久化线段树+LCA/树链剖分)
			题目链接 第一种方法,dfs序上建可持久化线段树,然后询问的时候把两点之间的所有树链扒出来做差. #include<bits/stdc++.h> using namespace std; ... 
- sed----Linux下文本处理五大神器之一
			转自:http://www.cnblogs.com/dong008259/archive/2011/12/07/2279897.html sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行 ... 
- BZOJ5336: [TJOI2018]party
			BZOJ5336: [TJOI2018]party https://lydsy.com/JudgeOnline/problem.php?id=5336 分析: 好题. 正常的思路是设\(f[i][j] ... 
- python实现进程的并发
			__author__ = 'luozt' import telnetlib import multiprocessing import random def telnet(ip,hostname): ... 
- CAS单点登录系统简介
			一.cas简介 全名:Central Authentication Service特点: 1.开源的.多协议的 SSO 解决方案: Protocols : Custom Protocol . CAS ... 
- 在C#中实现截获shell程序的输出
			在Windows环境下的所谓shell程序就是dos命令行程序,比如VC的CL.exe命令行编译器,JDK的javac编译器,启动java程序用的java.exe都是标准的shell程序.截获一个sh ... 
- IDEA运行Java的项目出现页面空白
			问题效果: 解决方案: 在发布的时候不应该把Tomcat的jar打包入内. 
- 解决 ASP.NET Chart 控件出错 为 ChartImg.axd 执行子请求时出错
			今天在做一个关于MVC的MSChart时,本以为很简单的一个东西,后面把数据什么的都绑定好后,满以为OK了,一运行就报错“ ASP.NET Chart 控件出错 为 ChartImg.axd ... 
- appium_python 实现手势密码
			直接上代码吧: from appium.webdriver.common.touch_action import TouchAction from driver import AppiumTest # ... 
- Http服务端
			第一,使用node提供的http模块 var http=require('http'); 第二,创建一个服务器实例 通过http的createServer()方法. var server=http.c ... 
