Mapping Table-Valued Functions (TVFs) using Entity Framework Database First
Entity
Framework 5 brings number of improvements and Mapping Table-Valued Functions
(TVFs) Support is one of them.
In this post I’ll follow the simple steps to setup ‘SQL Server Database’ and will create a Table-valued Function, will test it and then will use this in Console Application with Entity Framework Database First Workflow. Please note, TVFs are currently only supported in the Database First workflow.
In this post I’ll follow the simple steps to setup ‘SQL Server Database’ and will create a Table-valued Function, will test it and then will use this in Console Application with Entity Framework Database First Workflow. Please note, TVFs are currently only supported in the Database First workflow.
Step 1: Setup SQL Server Database
& TVF
I
recommend you to read this post [http://tinyurl.com/8hh8bdr] before proceeding. In the image
given below, I’ve create a ‘College’ Database and a ‘Students’ table and then a 'Table-valued Function’ by name ‘GetStudentName’ that will accept ‘Id’ as a parameter
and will return matching records in Tabular form.
There
are following key differences between TVFs and Stored Procedures:
- The results of a TVF is composable within LINQ Query while the results of a Stored Procedure cannot.
- Table-valued Functions can return only a single result set in Tabular form while Stored Procedures can return multiple result sets.
In above step we have created TVF and now in this step we will test it before consuming it in any application. Create
a new query and type the code as given in image and click on ‘Execute’.
Remember, we need to pass a parameter that’s why I have written ‘GetStudentName(1)’
here ‘1’ is my parameter.
Step 3: Create a Console Application
Create
a new console application File > New > Project > Visual C# >
Console Application.
Step 4: Add ADO.NET Entity Data Model
As,
we know TVF in EF5 only available for Database First workflow and we have
database now. So, to use this database in EF, we need to add ADO.NET Entity Data
Model.
- Right-click the project name in Solution Explorer, point to Add, and then click New Item.
- Select Data from the left menu and then select ADO.NET Entity Data Model in the Templates pane.
- Enter Model1.edmx for the file name, and then click Add.
- In the Choose Model Contents dialog box, select Generate from database, and then click Next.
- Click New Connection.
- Select Server name & Database Name and click OK.
- In the Choose Your Database Objects dialog box, under the Tables node, select the ‘Students’ tables and select ‘GetStudentName’ function located under the Stored Procedures and Functions node. Click Finish.
- In case if you want to map the results of the ‘GetStudentName’ function to the ‘Students’ entity, do this:
- Right-click the design surface and select Model Browser.
- In Model Browser, select Function Imports, and then double-click the ‘GetStudentName’ function.
- In the Edit Function Import dialog box, select Entities and choose ‘Students’.
You
will have following structure.
Step 5: What is Composable?
Composable
means, results from a TVF can be used in a LINQ query while the results of a
stored procedure cannot. That’s cool.
Step 6: Complete Code
Find
the complete code of my demo.
using System;
using System.Linq;
namespace ConsoleApplication5_TVF
{
class Program
{
static void Main(string[] args)
{
using (var context = new CollegeEntities())
{
var Id = 1;
var student = from s in context.GetStudentName(Id)
select new { s.Name };
foreach (var result in student)
{
Console.WriteLine("Student Name
is " + result.Name);
}
Console.ReadKey();
}
}
}
}
Output:
Student Name is abhimanyu
I
hope you like it. Thanks.
How can i know wich version of EF I'm using?
ReplyDeleteBecause I need to map a TVF but it does not appear into the chooseable objects in my edmx.
In the Solution Explorer > Expand References folder > Select the binary System.Data.Entity > then check the runtime version number in the Properties Explorer.
Delete