CodeFirstFunctions icon indicating copy to clipboard operation
CodeFirstFunctions copied to clipboard

Cast exception with models that have complex types

Open krispharper opened this issue 8 years ago • 6 comments

If a model has a property that is a complex type, this line throws an invalid cast exception.

I tried to modify the code to flatten out the first layer of a complex type (i.e., assuming all properties of the complex type are scalar properties) but I ran into an issue when trying to add the function to the ConceptualStoreMapping.

krispharper avatar Feb 21 '17 22:02 krispharper

Looks like a dupe of https://github.com/moozzyk/CodeFirstFunctions/issues/1. The discussion points to the same line...

moozzyk avatar Feb 22 '17 00:02 moozzyk

Ah, so it is. Do you know, offhand, if this sort of thing is actually supported by Entity Framework? When I've modified the code to create a store function with flattened properties, I got an error when calling AddFunctionImportMapping complaining that one of the properties wasn't a scalar property.

I could probably make the necessary updates, but I don't know enough about the situation at this point to know if it's even possible.

krispharper avatar Feb 22 '17 14:02 krispharper

It's been a long time so I am not 100% sure but I think EF does support TVFs returning entities with complex properties when using EDMX. This is the first part. The second part is the mapping. Even if EF does support TVFs with complex properties when using EDMX I don't know if it is possible to define such a mapping programmatically (i.e. without EDMX). I guess it is a matter of trying this and, if it does not work, sending a PR to the EF repo with necessary changes.

As a side note - there is already an issue about fixing/improving my changes to EF code that enabled TVFs with CodeFirst. This issue might be related.

moozzyk avatar Feb 23 '17 04:02 moozzyk

@krispharper - I tried to prototype support for complex properties (see: https://github.com/moozzyk/CodeFirstFunctions/compare/complex-properties) but unfortunately I was not able to make it work. This is the exception I got:

System.InvalidOperationException occurred
  HResult=-2146233079
  Message=The property 'Address' on the conceptual side is not a scalar property.
  Source=EntityFramework
  StackTrace:
       at System.Data.Entity.Core.Mapping.FunctionImportMappingComposable..ctor(EdmFunction functionImport, EdmFunction targetFunction, FunctionImportResultMapping resultMapping, EntityContainerMapping containerMapping)
       at CodeFirstStoreFunctions.FunctionsConvention.Apply(EntityContainer item, DbModel model) in C:\source\codefirstfunctions\CodeFirstStoreFunctions\FunctionsConvention.cs:line 41
       at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.ModelConventionDispatcher.Dispatch[T](T item)
       at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.ModelConventionDispatcher.VisitEdmEntityContainer(EntityContainer item)
       at System.Data.Entity.Edm.EdmModelVisitor.VisitCollection[T](IEnumerable`1 collection, Action`1 visitMethod)
       at System.Data.Entity.Edm.EdmModelVisitor.VisitEntityContainers(IEnumerable`1 entityContainers)
       at System.Data.Entity.Edm.EdmModelVisitor.VisitEdmModel(EdmModel item)
       at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.ModelConventionDispatcher.VisitEdmModel(EdmModel item)
       at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.ModelConventionDispatcher.Dispatch()
       at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.ApplyStoreModel(DbModel model)
       at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
       at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
       at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
       at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
  InnerException: 

Is this what you were seeing?

moozzyk avatar Apr 15 '17 19:04 moozzyk

Yeah that's exactly as far as I got (I believe it's thrown here). I was not brave enough to try and go about patching EF to make this work, as I'm assuming it would be a pretty sizable change and I'm not familiar with the EF codebase at all.

Thanks for taking a look though.

krispharper avatar Apr 16 '17 00:04 krispharper

Yeah, it might not be straightforward.

moozzyk avatar Apr 17 '17 04:04 moozzyk