Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TVF (Table Value Function) in entity framework using code-first

Does anyone know if it is possible to use TVFs in the entity framework using code-first? To be specific:

I know this is not supported for code-first currently (and it does not look like EF6 will allow it either). On the other hand, later versions of EF support TVFs in a database-first scenario (which is not an option for me). I am wondering if anyone know if there is some way to emulate what database first does by manipulating the model or something similar?

One further clarification; I know its possible to include TVFs by using SQL, but I also need the TVFs to be composable (i.e. be part of the entity LINQ statement).

like image 651
Kjetil Samuelsen Avatar asked Jun 03 '13 16:06

Kjetil Samuelsen


People also ask

How do you call a table-valued function in Entity Framework?

Step 1 − Select the Console Application from the middle pane and enter TableValuedFunctionDemo in the name field. Step 2 − In Server explorer right-click on your database. Step 3 − Select New Query and enter the following code in T-SQL editor to add a new table in your database.

How do I use code first in Entity Framework?

Step 1 − First, create the console application from File → New → Project… Step 2 − Select Windows from the left pane and Console Application from the template pane. Step 3 − Enter EFCodeFirstDemo as the name and select OK. Step 4 − Right-click on your project in the solution explorer and select Manage NuGet Packages…

How do you call a function in Entity Framework?

Step 1: Create an entity class which inherits “DbContext” class. Step 2: The following is the structure of the database with table and stored procedure. Step 3: Create a class to store the returned tabular value. Step 4: Create an object for the entity above and method to call a function.


1 Answers

This is now possible. I created a custom model convention which allows using store functions in CodeFirst in EF6.1. The convention is available on NuGet http://www.nuget.org/packages/EntityFramework.CodeFirstStoreFunctions. Here is the link to the blogpost containing all the details: http://blog.3d-logic.com/2014/04/09/support-for-store-functions-tvfs-and-stored-procs-in-entity-framework-6-1/

like image 121
Pawel Avatar answered Oct 12 '22 08:10

Pawel