Walkthrough: Table-Valued Functions (June CTP)
The information in this post is out of date.
Visit msdn.com/data/ef for the latest information on current and past releases of EF.
For Table-Valued Functions see https://msdn.com/data/hh859577
Microsoft Entity Framework June 2011 CTP is bringing support for Table-Valued Functions. TVFs are store functions capable of returning table-shaped values. TVFs are similar to Stored Procedures in that they can have procedural code in their body. Unlike stored procedures however, TVFs are composable which means I can use them inside a classic query. In this walkthrough we will learn how to map entities to TVFs using the VS designer. Then we will see how to use TVFs in LINQ queries.
- Microsoft Entity Framework June 2011 CTP. Click here to download.</![IF>
- Microsoft Entity Framework Tools June 2011 CTP. Click here to download.
- Northwind Database. Click here to download.
1. After installing the Northwind msi, execute "C:\SQL Server 2000 Sample Databases\instnwnd.sql" against your SQL Server instance.</![IF>
2. Now we will add a TVF which returns the details for a given order. Execute the following SQL Statement against the Northwind database.</![IF>
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. |
|
Setting up Project
-
Launch Visual Studio and create a new C# Console application.
- Make sure you are targeting the Entity Framework June 2011 CTP. Please see the EF June 2011 CTP Intro Post to learn how to change the target framework.
Creating a Model
1
-
Add a new ADO.NET Entity Data Model to your project by right clicking on the project and navigating to Add > New Item.
-
In the Add New Item window, click Data, and then click ADO.NET Entity Data Model. Name your model ‘NorthwindModel’ and click Add.
-
Select New Connection… on the Wizard. Under Server name, enter the name of your SQL Server instance (if you installed SQL Express, the server name should be .\SqlExpress)
-
Under Connect to a database, select Northwind from the first dropdown menu.
-
The Connection Properties window should look as follows. Click OK to proceed.
-
Click Next on the Entity Data Model Wizard, then expand the Tables node, then the dbo node. Select the checkboxes next to the Orders, Order Details, and Products tables. Now scroll down to find the Stored Procedures and Functions node, expand dbo, and select the checkbox next to GetDetailsForOrder from the list of functions. Click Finish.
The resulting Model includes three entities named Order, Order_Detail, and Product. There are one-to-many relationships between Order to Order_Detail, and between Product and Order_Detail.
</![IF>
Creating a FunctionImport for the TVF
-
Open the Model Browser by right clicking on the designer and selecting Model Browser:
- Find the GetDetailsForOrder function under NorthwindModel.Store:
-
Double-click on the function to bring up the Add Function Import dialog.
-
Click on the Function Import is Composable? checkbox
-
Select GetDetailsForOrderfrom the Stored Procedure/Function Name checkbox.
-
Click the Entities radio button then select Order_Detail from the dropdown. The Add Function Import dialog should now look as follows:
-
Click OK to close the dialog and create the Function Import. You will see it on the Model Browser under the Function Imports node:
Writing the App
1</![IF>
-
- Open Program.cs and enter the following code:
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. |
|
In the code above we create a context, then we create variables for the OrderID and the Minimum unit price we are looking for. Note how we can follow the TVF call with a where clause. This demonstrates the composable nature of TVFs. The output of our program is the following:
Products in order 10248 with unit price over $10:
Queso Cabrales
Mozzarella di Giovanni
Done! Press ENTER to exit.
EDMX
A TVF is represented as a function in the storage layer. The function is marked as IsComposable=”True”, and a ReturnType is specified to be a collection of RowTypes. A FunctionImport is specified in the conceptual layer. Here we specify the parameters’ EDM Types, EntitySet and ReturnType that the function will map to. The mapping layer glues the Function and the FunctionImport together.
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. |
|
.NET Layer
The Entity Framework tools generate a function stub so that the function can be used in LINQ queries:
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. |
|
Entity Framework June 2011 CTP does not include Code First support for TVFs. However, you can use DbContext against your TVFs. You can do this by adding the DbContext template to your model. The steps to add the template are the following:
-
Open NorthwindModel.edmx and right click on the canvas
-
Click on Add Code Generation Item…
-
Select ADO.NET DbContext Generator V4.2, enter a name for your template, and click Add
In this walkthrough we created a TVF in Northwind, then we created a FunctionImport and called the TVF within a LINQ query. If you would like to find out more about TVFs, please read the EF Design post about TVF Support. As always, we look forward to hearing from you so please leave your questions and comments below.
Pedro Ardila
Program Manager – Entity Framework
Anonymous
August 31, 2011
Hi, Thanks for this post, I have just discovered TVFs and it looks great! Is there any plan to include this feature into Code First approach? If yes, when? For the moment, I can not understand the workaround you have proposed: with CodeFirst, there is no edmx file, so how can I do the "Add Code Generation item" step? Should I create a edmx model aside from my POCOs?Anonymous
October 31, 2011
When will TVFs be supported for Code First?