Dixin / EntityFramework.Functions

EntityFramework.Functions library implements Entity Framework code first support for stored procedures (with single result type, multiple result types, output parameter), table-valued functions (returning entity type, complex type), scalar-valued functions (composable, non-composable), aggregate functions, built-in functions, niladic functions, and model defined functions.
https://weblogs.asp.net/Dixin/EntityFramework.Functions
MIT License
79 stars 27 forks source link

Return Entity from a Table Valued Function #7

Closed samunro closed 8 years ago

samunro commented 8 years ago

I have been getting the following error and I narrowed it down to returning an entity - as opposed to a complex type - from a table valued function.

Exception thrown: 'System.InvalidOperationException' in EntityFramework.dll

Additional information: The key properties of all entity types returned by the function import 'Context.GetPlatformType()' must be mapped to the same non-nullable columns returned by the storage function.

If I return a complex type from the function instead of the entity then all works as expected. I was even able to take the class that was originally an entity, register it as a complex type (after removing the DbSet property on the context to prevent it from being registered as an entity) and that worked too.

Is there anything that I can do to be able to return an entity from a table valued function? Is this a feature that is planned? Any idea how far off it might be?

Many thanks,

Scott

Dixin commented 8 years ago

Thank you for bring up this issue. I spent quite some time to investigate, but I do not have a fix at this moment.

Entity Framework database first supports table valued function to return entity type. To make this work in code first, a proper API is needed to build an empty C-S mapping like this:

<FunctionImportMapping FunctionName="Model.Store.ufnGetCategoryProducts" FunctionImportName="ufnGetCategoryProducts">
</FunctionImportMapping>

Unfortunately, I was not able to find an EDM API to do that. I also read the source code of Entity Framework, got no luck. It seems using complex type is the only option for this scenario. Sorry for the inconvenience.

Dixin commented 8 years ago

Found a different approach to workaround the issue. A new version is released to nuget. Please have a try.