efcore icon indicating copy to clipboard operation
efcore copied to clipboard

How to apply ValueConverters to the properties of interface type?

Open IvaskevychYuriy opened this issue 6 years ago • 5 comments

Probably an addition to this issue.

Details

Suppose I have the following entity:

class Order
{
   [Key]
   public int Id { get; set; }

   public IData Data { get; set; }
}

where IData is an empty interface (it can be thought of as a marker or a placeholder etc) like so:

interface IData
{
}

Then I have a ValueConverter that does the convertion of IData to string representation and vice versa, determining a concrete implementation on the fly in case of reading from DB. That converter is configured in model:

modelBuilder.Entity<Order>()
   .Property(x => x.Data)
   .HasConversion(new MyDataValueConverter(...));

Issue

While it should work for concrete classes as in the linked issue, it doesn't for interface properties. When trying to execute a select for example, it produces the following error:

"The property 'Order.Data' is of an interface type ('IData'). If it is a navigation property manually configure the relationship for this property by casting it to a mapped entity type, otherwise ignore the property using the NotMappedAttribute or 'EntityTypeBuilder.Ignore' in 'OnModelCreating'."

Relevant part of the stacktrace is (considering TEntity to actually be Order):

at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.PropertyMappingValidationConvention.Apply(InternalModelBuilder modelBuilder) at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.ImmediateConventionScope.OnModelBuilt(InternalModelBuilder modelBuilder) at Microsoft.EntityFrameworkCore.ModelBuilder.FinalizeModel() at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode) at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor) at System.Lazy`1.CreateValue() at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel() at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model() at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite singletonCallSite, RuntimeResolverContext context) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite singletonCallSite, RuntimeResolverContext context) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider) at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies() at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider() at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies() at Microsoft.EntityFrameworkCore.DbContext.Set[TEntity]()

Provider and version information

EF Core version: 2.2.6 Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: .NET Core 2.2 Operating system: Windows 10 x64 IDE: Visual Studio 2019 16.7

Question

Given that a custom converter knows how to correctly create an IData implementation, I would suppose this as a valid use-case. Is it possible to achieve the desired result?

IvaskevychYuriy avatar Nov 05 '20 19:11 IvaskevychYuriy

@IvaskevychYuriy EF Core 2.2 is out-of-support. Please try updating to EF Core 3.1 or EF Core 5.0 RC2 and report back if you're still seeing the issue.

ajcvickers avatar Nov 05 '20 22:11 ajcvickers

@ajcvickers Hi, just tried a small test project with EF Core 3.1 - there is no such problem. I think it could be originated elsewhere in the original application, so I guess I'll close this particular issue unless I find smth else. Thanks for a quick response and sorry for bothering!

IvaskevychYuriy avatar Nov 06 '20 09:11 IvaskevychYuriy

I'm on .Net 6 and I get the same issue with the same cause.

sellorio2 avatar Sep 01 '22 23:09 sellorio2

@sellorio2 Please open a new issue and attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.

ajcvickers avatar Sep 01 '22 23:09 ajcvickers

My mistake, incorrect code caused the EF Core logic to pull in an unexpected class which resulted in a similar error. I suspect OP had the same mistake in their code all that time ago.

sellorio2 avatar Sep 02 '22 00:09 sellorio2