Entity Framework Tutorial Basics(34):Table-Valued Function

來源:互聯網
上載者:User

標籤:

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(1).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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.