Generating/using table valued functions in Entity Framework 6.x with the help of EF Reverse POCO Generator

c# code-generation entity-framework-6 poco t4


In my project I rely heavily on the EF Reverse POCO generator by Simon Hughes.

I have a TVF GetItemSummaries that takes 4 parameters and returns a table. This TVF is actually a replecement for a view (and the generator did not recognize the view due to lack of a PK, I assume). Now, running that in SQL Mgnt Studio works fine!

I want to add that TVF to the .tt template but I'm having trouble setting that up. There is (almost) no information on how to do this.

  1. First of, I am separating the POCOs from the DBContext and *Configuration classes, and also the IDBContext interface.

What I've done so far:

  1. I set IncludeTableValuedFunctions = true; and include the "EntityFramework.CodeFirstStoreFunctions" Nuget Package as instructed. From there, I am lost.

  2. I tried to include the TVF names in the TableFilterInclude and StoredProcedureFilterInclude filter Regexes. No luck. The filter works for tables, but not for TVFs.

  3. Then I tried adding the actual functions manually to the DBContext and IDBContext classes, but the returned type is a custom type of type ItemSummary, which wasn't defined. Etc. etc.

This is what I have added manually to the DbContext (which I hoped would be generated for me), but still doesn't work, sinece the DbContext doesn't know about the ItemSummary type:

partial void OnModelCreatingPartial( DbModelBuilder modelBuilder )
    modelBuilder.Conventions.Add( new FunctionsConvention<BusinessContext>( "dbo" ) );

[DbFunction( "BusinessContext", "FindItemSummaries" )]
public IQueryable<ItemSummary> FindItemSummaries( Guid customerId, DateTime startDate, DateTime endDate, int programId )
    var customerParameter = new ObjectParameter( "customerId", customerId );
    var startDateParameter = new ObjectParameter( "startDate", startDate );
    var endDateParameter = new ObjectParameter( "endDate", endDate );
    var programIdParameter = new ObjectParameter( "programId", programId );

    var oc = ( ( IObjectContextAdapter ) this ).ObjectContext;
    var query = String.Format( "[{0}].{1}", GetType().Name, "[FindItemSummaries](@customerId, @startDate, @endDate, @programId)" );

    var result = oc.CreateQuery<ItemSummary>( query, customerParameter, startDateParameter, endDateParameter, programIdParameter );

    return result;

Bottom line is, I do not understand how to set this up. Has anyone done this before? How and where does "EntityFramework.CodeFirstStoreFunctions" Nuget Package come into play? I do have read their blog post on that topic

Thank you so much.

2/12/2016 8:26:58 AM

Accepted Answer

This was resolved after updating the POCO Generator to version 2.18.1 The TVFs then appeared in the DbContext as expected.

2/12/2016 8:06:21 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow