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.
What I've done so far:
I set IncludeTableValuedFunctions = true;
and include the "EntityFramework.CodeFirstStoreFunctions" Nuget Package as instructed.
From there, I am lost.
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.
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.
This was resolved after updating the POCO Generator to version 2.18.1 The TVFs then appeared in the DbContext as expected.