Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constructor not found for type 'Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NullableClassCurrentProviderValueComparer #32411

Closed
kskalski opened this issue Nov 26, 2023 · 11 comments · Fixed by #32560
Labels
area-model-building closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported regression Servicing-approved type-bug
Milestone

Comments

@kskalski
Copy link

I'm trying to migrate from .NET Core 7 to .NET Core 8 and EF fails to validate my model throwing exception

Include your code

Since the exception relates to comparers I suppose it might be related to one of my custom comparers like:

    static readonly ValueComparer<ByteString> BYTESTRING_COMPARER = new ValueComparer<ByteString>(
      (a, b) => b != null && a.SequenceEqual(b), a => a.GetHashCode());

    static readonly ValueComparer<Timestamp> TIMESTAMP_COMPARER = new ValueComparer<Timestamp>(
      (a, b) => a.Equals(b), a => a.GetHashCode(), a => a.Clone());
...
        b.Property(e => e.ScheduledForTime).HasConversion(TIMESTAMP_TOTALMS_N_EXPR, TOTALMS_N_TIMESTAMP_EXPR, TIMESTAMP_COMPARER).HasColumnType("bigint");

Include stack traces

I get this exception on startup:

Application startup exception: System.MissingMethodException: Constructor on type 'Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NullableClassCurrentProviderValueComparer`2[[System.Collections.Generic.IEnumerable`1[[System.Byte, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.String, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]' not found.
   at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture)
   at System.Activator.CreateInstance(Type type, Object[] args)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.CurrentValueComparerFactory.Create(IPropertyBase propertyBase)
   at Microsoft.EntityFrameworkCore.Metadata.Internal.PropertyBase.<>c.<get_CurrentValueComparer>b__49_0(PropertyBase property)
   at Microsoft.EntityFrameworkCore.Internal.NonCapturingLazyInitializer.EnsureInitialized[TParam,TValue](TValue& target, TParam param, Func`2 valueFactory)
   at Microsoft.EntityFrameworkCore.Metadata.Internal.PropertyBase.get_CurrentValueComparer()
   at Microsoft.EntityFrameworkCore.Metadata.Internal.PropertyBase.Microsoft.EntityFrameworkCore.Metadata.IPropertyBase.GetCurrentValueComparer()
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.<ValidateTypeMappings>g__Validate|25_0(ITypeBase typeBase, IDiagnosticsLogger`1 logger)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.ValidateTypeMappings(IModel model, IDiagnosticsLogger`1 logger)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger)
   at Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger)
   at Microsoft.EntityFrameworkCore.Sqlite.Infrastructure.Internal.SqliteModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelRuntimeInitializer.Initialize(IModel model, Boolean designTime, IDiagnosticsLogger`1 validationLogger)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext context, ModelCreationDependencies modelCreationDependencies, Boolean designTime)
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel(Boolean designTime)
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()
   at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.<>c.<TryAddCoreServices>b__8_4(IServiceProvider p)
   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 callSite, 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 callSite, 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 callSite, 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 callSite, 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 callSite, 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 callSite, 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.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass2_0.<RealizeService>b__0(ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(ServiceIdentifier serviceIdentifier, ServiceProviderEngineScope serviceProviderEngineScope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
   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_ContextServices()
   at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
   at Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.get_Instance()
   at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.get_Instance()
   at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.GetRelationalService[TService](IInfrastructure`1 databaseFacade)
   at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.Migrate(DatabaseFacade databaseFacade)
   at Pro.Elector.Kuropatwa.Startup.Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime lifetime, IServiceProvider serviceProvider, ConvoysManager convoys, ApplicationDbContext dataContext) in C:\rep\Kogut\src\Kuropatwa\Startup.cs:line 268
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeWithManyArgs(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Microsoft.AspNetCore.Hosting.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder)
   at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.Configure(IApplicationBuilder app)
   at Microsoft.AspNetCore.Hosting.WebHost.BuildApplication()
2023-11-26 02:17:40,716 [1] FATAL AspNetCore.Hosting.Diagnostics - Application startup exception
System.MissingMethodException: Constructor on type 'Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NullableClassCurrentProviderValueComparer`2[[System.Collections.Generic.IEnumerable`1[[System.Byte, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.String, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]' not found.
   at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture)
   at System.Activator.CreateInstance(Type type, Object[] args)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.CurrentValueComparerFactory.Create(IPropertyBase propertyBase)
   at Microsoft.EntityFrameworkCore.Metadata.Internal.PropertyBase.<>c.<get_CurrentValueComparer>b__49_0(PropertyBase property)
   at Microsoft.EntityFrameworkCore.Internal.NonCapturingLazyInitializer.EnsureInitialized[TParam,TValue](TValue& target, TParam param, Func`2 valueFactory)
   at Microsoft.EntityFrameworkCore.Metadata.Internal.PropertyBase.get_CurrentValueComparer()
   at Microsoft.EntityFrameworkCore.Metadata.Internal.PropertyBase.Microsoft.EntityFrameworkCore.Metadata.IPropertyBase.GetCurrentValueComparer()
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.<ValidateTypeMappings>g__Validate|25_0(ITypeBase typeBase, IDiagnosticsLogger`1 logger)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.ValidateTypeMappings(IModel model, IDiagnosticsLogger`1 logger)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger)
   at Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger)
   at Microsoft.EntityFrameworkCore.Sqlite.Infrastructure.Internal.SqliteModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelRuntimeInitializer.Initialize(IModel model, Boolean designTime, IDiagnosticsLogger`1 validationLogger)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext context, ModelCreationDependencies modelCreationDependencies, Boolean designTime)
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel(Boolean designTime)
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()
   at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.<>c.<TryAddCoreServices>b__8_4(IServiceProvider p)
   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 callSite, 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 callSite, 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 callSite, 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 callSite, 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 callSite, 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 callSite, 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.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass2_0.<RealizeService>b__0(ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(ServiceIdentifier serviceIdentifier, ServiceProviderEngineScope serviceProviderEngineScope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
   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_ContextServices()
   at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
   at Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.get_Instance()
   at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.get_Instance()
   at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.GetRelationalService[TService](IInfrastructure`1 databaseFacade)
   at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.Migrate(DatabaseFacade databaseFacade)
   at Pro.Elector.Kuropatwa.Startup.Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime lifetime, IServiceProvider serviceProvider, ConvoysManager convoys, ApplicationDbContext dataContext) in C:\rep\Kogut\src\Kuropatwa\Startup.cs:line 268
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeWithManyArgs(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Microsoft.AspNetCore.Hosting.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder)
   at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.Configure(IApplicationBuilder app)
   at Microsoft.AspNetCore.Hosting.WebHost.BuildApplication()
Unhandled exception. System.MissingMethodException: Constructor on type 'Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NullableClassCurrentProviderValueComparer`2[[System.Collections.Generic.IEnumerable`1[[System.Byte, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.String, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]' not found.
   at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture)
   at System.Activator.CreateInstance(Type type, Object[] args)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.CurrentValueComparerFactory.Create(IPropertyBase propertyBase)
   at Microsoft.EntityFrameworkCore.Metadata.Internal.PropertyBase.<>c.<get_CurrentValueComparer>b__49_0(PropertyBase property)
   at Microsoft.EntityFrameworkCore.Internal.NonCapturingLazyInitializer.EnsureInitialized[TParam,TValue](TValue& target, TParam param, Func`2 valueFactory)
   at Microsoft.EntityFrameworkCore.Metadata.Internal.PropertyBase.get_CurrentValueComparer()
   at Microsoft.EntityFrameworkCore.Metadata.Internal.PropertyBase.Microsoft.EntityFrameworkCore.Metadata.IPropertyBase.GetCurrentValueComparer()
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.<ValidateTypeMappings>g__Validate|25_0(ITypeBase typeBase, IDiagnosticsLogger`1 logger)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.ValidateTypeMappings(IModel model, IDiagnosticsLogger`1 logger)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger)
   at Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger)
   at Microsoft.EntityFrameworkCore.Sqlite.Infrastructure.Internal.SqliteModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelRuntimeInitializer.Initialize(IModel model, Boolean designTime, IDiagnosticsLogger`1 validationLogger)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext context, ModelCreationDependencies modelCreationDependencies, Boolean designTime)
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel(Boolean designTime)
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()
   at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.<>c.<TryAddCoreServices>b__8_4(IServiceProvider p)
   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 callSite, 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 callSite, 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 callSite, 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 callSite, 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 callSite, 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 callSite, 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.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass2_0.<RealizeService>b__0(ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(ServiceIdentifier serviceIdentifier, ServiceProviderEngineScope serviceProviderEngineScope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
   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_ContextServices()
   at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
   at Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.get_Instance()
   at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.get_Instance()
   at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.GetRelationalService[TService](IInfrastructure`1 databaseFacade)
   at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.Migrate(DatabaseFacade databaseFacade)
   at Pro.Elector.Kuropatwa.Startup.Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime lifetime, IServiceProvider serviceProvider, ConvoysManager convoys, ApplicationDbContext dataContext) in C:\rep\Kogut\src\Kuropatwa\Startup.cs:line 268
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeWithManyArgs(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Microsoft.AspNetCore.Hosting.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder)
   at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.Configure(IApplicationBuilder app)
   at Microsoft.AspNetCore.Hosting.WebHost.BuildApplication()
   at Microsoft.AspNetCore.Hosting.WebHost.StartAsync(CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Hosting.WebHostExtensions.RunAsync(IWebHost host, CancellationToken token, String startupMessage)
   at Microsoft.AspNetCore.Hosting.WebHostExtensions.RunAsync(IWebHost host, CancellationToken token, String startupMessage)
   at Microsoft.AspNetCore.Hosting.WebHostExtensions.RunAsync(IWebHost host, CancellationToken token)
   at Microsoft.AspNetCore.Hosting.WebHostExtensions.Run(IWebHost host)

Include provider and version information

EF Core version: 8.0.0
Database provider: Microsoft.EntityFrameworkCore.Sqlite (8.0.0) / Npgsql.EntityFrameworkCore.PostgreSQL (8.0.0)
Target framework: .NET 8
Operating system: Windows 11

@ErikEJ
Copy link
Contributor

ErikEJ commented Nov 26, 2023

Please share your .csproj at least.

@kskalski
Copy link
Author

Here is a slightly cleaned-up csproj file
EFBug.csproj.gz

At this point it's a bit inpractical for me to post a complete repro - was hoping for a quick insight what might be the issue, maybe indeed it's something about dependencies. When needed I will work on isolating the problem further.

@alexeykorsakov
Copy link

Having the same issue when upgrading from net6 to net8

@roji
Copy link
Member

roji commented Nov 27, 2023

This kind of issue is almost always the result of mixing an old version of an EF provider with a new version of EF itself (or vice versa). @kskalski I can't see something immediately wrong with the csproj you posted, but that's just an isolated csproj and is incomplete (i.e. there may be dependencies doing something there). I'd recommend looking at the entire dependency graph after dotnet restore (e.g. via obj/project.assets.json).

If you can't figure it out, then yes, we'll need a minimal, runnable code sample that shows the problem in order to investigate.

@ErikEJ
Copy link
Contributor

ErikEJ commented Nov 27, 2023

Or use dotnet list package --include-transitive

@roji
Copy link
Member

roji commented Nov 27, 2023

Didn't know that one, thanks @ErikEJ!

@kskalski
Copy link
Author

The transitive package list is below. I can see some packages held by at 7, so this might be related... will investigate some of those deps later on

   [net8.0]:
   Pakiet najwyższego poziomu                               Żądane   Rozpoznane
   > log4net                                                2.0.15   2.0.15
   > Microsoft.AspNetCore.Identity.EntityFrameworkCore      8.0.0    8.0.0
   > Microsoft.AspNetCore.Mvc.NewtonsoftJson                8.0.0    8.0.0
   > Microsoft.Bcl.AsyncInterfaces                          8.0.0    8.0.0
   > Microsoft.EntityFrameworkCore.Design                   8.0.0    8.0.0
   > Microsoft.EntityFrameworkCore.Sqlite                   8.0.0    8.0.0
   > Microsoft.Extensions.Localization                      8.0.0    8.0.0
   > Microsoft.Extensions.Logging.Log4Net.AspNetCore        7.0.0    7.0.0
   > Microsoft.VisualStudio.Web.CodeGeneration.Design       8.0.0    8.0.0
   > Npgsql.EntityFrameworkCore.PostgreSQL                  8.0.0    8.0.0
   > prometheus-net.AspNetCore                              8.1.0    8.1.0
   > System.Text.Encoding.CodePages                         8.0.0    8.0.0
   > VueCliMiddleware                                       6.0.0    6.0.0

   Pakiet przechodni                                                    Rozpoznane
   > FFMediaToolkit                                                     4.4.1
   > FFmpeg.AutoGen                                                     5.0.0
   > Google.Api.CommonProtos                                            2.5.0
   > Google.Api.Gax                                                     3.7.0
   > Google.Api.Gax.Grpc                                                3.7.0
   > Google.Api.Gax.Grpc.GrpcCore                                       3.7.0
   > Google.Apis                                                        1.57.0
   > Google.Apis.Auth                                                   1.57.0
   > Google.Apis.Core                                                   1.57.0
   > Google.Cloud.Speech.V1                                             2.9.0
   > Google.LongRunning                                                 2.3.0
   > Google.Protobuf                                                    3.24.3
   > Grpc.Auth                                                          2.46.6
   > Grpc.Core                                                          2.46.6
   > Grpc.Core.Api                                                      2.46.6
   > Humanizer                                                          2.14.1
   > Humanizer.Core                                                     2.14.1
   > Humanizer.Core.af                                                  2.14.1
   > Humanizer.Core.ar                                                  2.14.1
   > Humanizer.Core.az                                                  2.14.1
   > Humanizer.Core.bg                                                  2.14.1
   > Humanizer.Core.bn-BD                                               2.14.1
   > Humanizer.Core.cs                                                  2.14.1
   > Humanizer.Core.da                                                  2.14.1
   > Humanizer.Core.de                                                  2.14.1
   > Humanizer.Core.el                                                  2.14.1
   > Humanizer.Core.es                                                  2.14.1
   > Humanizer.Core.fa                                                  2.14.1
   > Humanizer.Core.fi-FI                                               2.14.1
   > Humanizer.Core.fr                                                  2.14.1
   > Humanizer.Core.fr-BE                                               2.14.1
   > Humanizer.Core.he                                                  2.14.1
   > Humanizer.Core.hr                                                  2.14.1
   > Humanizer.Core.hu                                                  2.14.1
   > Humanizer.Core.hy                                                  2.14.1
   > Humanizer.Core.id                                                  2.14.1
   > Humanizer.Core.is                                                  2.14.1
   > Humanizer.Core.it                                                  2.14.1
   > Humanizer.Core.ja                                                  2.14.1
   > Humanizer.Core.ko-KR                                               2.14.1
   > Humanizer.Core.ku                                                  2.14.1
   > Humanizer.Core.lv                                                  2.14.1
   > Humanizer.Core.ms-MY                                               2.14.1
   > Humanizer.Core.mt                                                  2.14.1
   > Humanizer.Core.nb                                                  2.14.1
   > Humanizer.Core.nb-NO                                               2.14.1
   > Humanizer.Core.nl                                                  2.14.1
   > Humanizer.Core.pl                                                  2.14.1
   > Humanizer.Core.pt                                                  2.14.1
   > Humanizer.Core.ro                                                  2.14.1
   > Humanizer.Core.ru                                                  2.14.1
   > Humanizer.Core.sk                                                  2.14.1
   > Humanizer.Core.sl                                                  2.14.1
   > Humanizer.Core.sr                                                  2.14.1
   > Humanizer.Core.sr-Latn                                             2.14.1
   > Humanizer.Core.sv                                                  2.14.1
   > Humanizer.Core.th-TH                                               2.14.1
   > Humanizer.Core.tr                                                  2.14.1
   > Humanizer.Core.uk                                                  2.14.1
   > Humanizer.Core.uz-Cyrl-UZ                                          2.14.1
   > Humanizer.Core.uz-Latn-UZ                                          2.14.1
   > Humanizer.Core.vi                                                  2.14.1
   > Humanizer.Core.zh-CN                                               2.14.1
   > Humanizer.Core.zh-Hans                                             2.14.1
   > Humanizer.Core.zh-Hant                                             2.14.1
   > Microsoft.AspNetCore.Cryptography.Internal                         8.0.0
   > Microsoft.AspNetCore.Cryptography.KeyDerivation                    8.0.0
   > Microsoft.AspNetCore.JsonPatch                                     8.0.0
   > Microsoft.AspNetCore.Razor.Language                                6.0.24
   > Microsoft.AspNetCore.SpaServices.Extensions                        6.0.0
   > Microsoft.Build                                                    17.7.2
   > Microsoft.Build.Framework                                          17.7.2
   > Microsoft.CodeAnalysis.Analyzers                                   3.3.4
   > Microsoft.CodeAnalysis.AnalyzerUtilities                           3.3.0
   > Microsoft.CodeAnalysis.Common                                      4.8.0-3.final
   > Microsoft.CodeAnalysis.CSharp                                      4.8.0-3.final
   > Microsoft.CodeAnalysis.CSharp.Features                             4.8.0-3.final
   > Microsoft.CodeAnalysis.CSharp.Workspaces                           4.8.0-3.final
   > Microsoft.CodeAnalysis.Elfie                                       1.0.0
   > Microsoft.CodeAnalysis.Features                                    4.8.0-3.final
   > Microsoft.CodeAnalysis.Razor                                       6.0.24
   > Microsoft.CodeAnalysis.Scripting.Common                            4.8.0-3.final
   > Microsoft.CodeAnalysis.Workspaces.Common                           4.8.0-3.final
   > Microsoft.CSharp                                                   4.7.0
   > Microsoft.Data.Sqlite.Core                                         8.0.0
   > Microsoft.DiaSymReader                                             2.0.0
   > Microsoft.DotNet.Scaffolding.Shared                                8.0.0
   > Microsoft.EntityFrameworkCore                                      8.0.0
   > Microsoft.EntityFrameworkCore.Abstractions                         8.0.0
   > Microsoft.EntityFrameworkCore.Analyzers                            8.0.0
   > Microsoft.EntityFrameworkCore.Relational                           8.0.0
   > Microsoft.EntityFrameworkCore.Sqlite.Core                          8.0.0
   > Microsoft.Extensions.Caching.Abstractions                          8.0.0
   > Microsoft.Extensions.Caching.Memory                                8.0.0
   > Microsoft.Extensions.Configuration                                 7.0.0
   > Microsoft.Extensions.Configuration.Abstractions                    8.0.0
   > Microsoft.Extensions.Configuration.Binder                          7.0.4
   > Microsoft.Extensions.DependencyInjection                           8.0.0
   > Microsoft.Extensions.DependencyInjection.Abstractions              8.0.0
   > Microsoft.Extensions.DependencyModel                               8.0.0
   > Microsoft.Extensions.FileProviders.Abstractions                    6.0.0
   > Microsoft.Extensions.FileProviders.Physical                        6.0.0
   > Microsoft.Extensions.FileSystemGlobbing                            6.0.0
   > Microsoft.Extensions.Http                                          3.1.0
   > Microsoft.Extensions.Identity.Core                                 8.0.0
   > Microsoft.Extensions.Identity.Stores                               8.0.0
   > Microsoft.Extensions.Localization.Abstractions                     8.0.0
   > Microsoft.Extensions.Logging                                       8.0.0
   > Microsoft.Extensions.Logging.Abstractions                          8.0.0
   > Microsoft.Extensions.ObjectPool                                    7.0.0
   > Microsoft.Extensions.Options                                       8.0.0
   > Microsoft.Extensions.Primitives                                    8.0.0
   > Microsoft.NET.StringTools                                          17.7.2
   > Microsoft.NETCore.Platforms                                        1.1.0
   > Microsoft.NETCore.Targets                                          1.1.0
   > Microsoft.VisualStudio.Web.CodeGeneration                          8.0.0
   > Microsoft.VisualStudio.Web.CodeGeneration.Core                     8.0.0
   > Microsoft.VisualStudio.Web.CodeGeneration.EntityFrameworkCore      8.0.0
   > Microsoft.VisualStudio.Web.CodeGeneration.Templating               8.0.0
   > Microsoft.VisualStudio.Web.CodeGeneration.Utils                    8.0.0
   > Microsoft.VisualStudio.Web.CodeGenerators.Mvc                      8.0.0
   > Microsoft.Win32.Primitives                                         4.3.0
   > Microsoft.Win32.SystemEvents                                       7.0.0
   > Mono.TextTemplating                                                2.3.1
   > Newtonsoft.Json                                                    13.0.3
   > Newtonsoft.Json.Bson                                               1.0.2
   > Npgsql                                                             8.0.0
   > NPOI                                                               2.5.6
   > NuGet.Common                                                       6.3.1
   > NuGet.Configuration                                                6.3.1
   > NuGet.DependencyResolver.Core                                      6.3.1
   > NuGet.Frameworks                                                   6.3.1
   > NuGet.LibraryModel                                                 6.3.1
   > NuGet.Packaging                                                    6.3.1
   > NuGet.ProjectModel                                                 6.3.1
   > NuGet.Protocol                                                     6.3.1
   > NuGet.Versioning                                                   6.3.1
   > Portable.BouncyCastle                                              1.8.9
   > prometheus-net                                                     8.1.0
   > runtime.native.System                                              4.3.0
   > SharpZipLib                                                        1.3.3
   > SQLitePCLRaw.bundle_e_sqlite3                                      2.1.6
   > SQLitePCLRaw.core                                                  2.1.6
   > SQLitePCLRaw.lib.e_sqlite3                                         2.1.6
   > SQLitePCLRaw.provider.e_sqlite3                                    2.1.6
   > System.Buffers                                                     4.5.1
   > System.CodeDom                                                     5.0.0
   > System.Collections                                                 4.3.0
   > System.Collections.Immutable                                       7.0.0
   > System.Composition                                                 7.0.0
   > System.Composition.AttributedModel                                 7.0.0
   > System.Composition.Convention                                      7.0.0
   > System.Composition.Hosting                                         7.0.0
   > System.Composition.Runtime                                         7.0.0
   > System.Composition.TypedParts                                      7.0.0
   > System.Configuration.ConfigurationManager                          7.0.0
   > System.Data.DataSetExtensions                                      4.5.0
   > System.Diagnostics.Debug                                           4.3.0
   > System.Diagnostics.EventLog                                        7.0.0
   > System.Diagnostics.Tracing                                         4.3.0
   > System.Drawing.Common                                              7.0.0
   > System.Formats.Asn1                                                5.0.0
   > System.Globalization                                               4.3.0
   > System.IO                                                          4.3.0
   > System.IO.Pipelines                                                7.0.0
   > System.Memory                                                      4.5.4
   > System.Net.NameResolution                                          4.3.0
   > System.Net.Primitives                                              4.3.0
   > System.Reflection                                                  4.3.0
   > System.Reflection.Metadata                                         7.0.0
   > System.Reflection.MetadataLoadContext                              7.0.0
   > System.Reflection.Primitives                                       4.3.0
   > System.Resources.ResourceManager                                   4.3.0
   > System.Runtime                                                     4.3.0
   > System.Runtime.CompilerServices.Unsafe                             6.0.0
   > System.Runtime.Extensions                                          4.3.0
   > System.Runtime.Handles                                             4.3.0
   > System.Runtime.InteropServices                                     4.3.0
   > System.Security.Claims                                             4.3.0
   > System.Security.Cryptography.Cng                                   5.0.0
   > System.Security.Cryptography.Pkcs                                  5.0.0
   > System.Security.Cryptography.ProtectedData                         7.0.0
   > System.Security.Permissions                                        7.0.0
   > System.Security.Principal                                          4.3.0
   > System.Security.Principal.Windows                                  4.3.0
   > System.Text.Encoding                                               4.3.0
   > System.Text.Encodings.Web                                          8.0.0
   > System.Text.Json                                                   8.0.0
   > System.Threading                                                   4.3.0
   > System.Threading.Channels                                          7.0.0
   > System.Threading.Tasks                                             4.3.0
   > System.Threading.Tasks.Dataflow                                    7.0.0
   > System.Windows.Extensions                                          7.0.0
   > TestableIO.System.IO.Abstractions                                  19.2.87
   > TestableIO.System.IO.Abstractions.Wrappers                         19.2.87

@ajcvickers
Copy link
Contributor

ajcvickers commented Nov 28, 2023

@kskalski There is nothing obviously wrong with your references, and I am unable to reproduce what you are seeing with the snippets posted, so can you please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.

@kskalski
Copy link
Author

kskalski commented Dec 1, 2023

It appears this structure of models triggers the error:

class Key {
    public string Str() { return ""; } 
    public static Key From(byte[] b) { return new Key(); }
}

class Parent {
    public Key Guid { get; set; }
}
class Child {
    public Key ParentGuid { get; set; }
    public long Id { get; set; }
}

public class ApplicationDbContext : IdentityDbContext {
   static readonly Expression<Func<Key, string>> KEY_STR_EXPR = v => v.Str();
   static readonly Expression<Func<string, Key>> STR_KEY_EXPR = v => Key.From(Convert.FromBase64String(v));
   static readonly ValueComparer<Key> KEY_COMPARER = new ValueComparer<Key>(
     (a, b) => b != null && a.Equals(b), a => a.GetHashCode());

   protected override void OnModelCreating(ModelBuilder modelBuilder) {
      base.OnModelCreating(modelBuilder);

      modelBuilder.UseIdentityByDefaultColumns();
      modelBuilder.Entity<Parent>(b => {
        b.Property(e => e.Guid).HasConversion(KEY_STR_EXPR, STR_KEY_EXPR, KEY_COMPARER);
        b.HasKey(e => e.Guid);
      });
      modelBuilder.Entity<Child>(b => {
        b.HasOne<Parent>().WithMany().HasForeignKey(e => e.ParentGuid).IsRequired();
        b.HasKey("ParentGuid", "Id");        
      });
   }
}

@ajcvickers
Copy link
Contributor

@kskalski I am not able to reproduce this--see my code below. This code requires only the following NuGet packages:

        <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0" />
        <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.0" />

Does the code run for you with just these packages? If so, I would start triangulating the difference between this and the code that fails.

using (var context = new ApplicationDbContext())
{
    await context.Database.EnsureDeletedAsync();
    await context.Database.EnsureCreatedAsync();
}

class Key
{
    public string Str()
    {
        return "";
    }

    public static Key From(byte[] b)
    {
        return new Key();
    }
}

class Parent
{
    public Key Guid { get; set; }
}

class Child
{
    public Key ParentGuid { get; set; }
    public long Id { get; set; }
}

public class ApplicationDbContext : IdentityDbContext
{
    static readonly Expression<Func<Key, string>> KEY_STR_EXPR = v => v.Str();
    static readonly Expression<Func<string, Key>> STR_KEY_EXPR = v => Key.From(Convert.FromBase64String(v));

    static readonly ValueComparer<Key> KEY_COMPARER = new ValueComparer<Key>(
        (a, b) => b != null && a.Equals(b), a => a.GetHashCode());

     protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
         => optionsBuilder
             .UseNpgsql("MyConnectionString")
             .LogTo(Console.WriteLine, LogLevel.Information)
             .EnableSensitiveDataLogging();
    
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.UseIdentityByDefaultColumns();
        modelBuilder.Entity<Parent>(b =>
        {
            b.Property(e => e.Guid).HasConversion(KEY_STR_EXPR, STR_KEY_EXPR, KEY_COMPARER);
            b.HasKey(e => e.Guid);
        });
        modelBuilder.Entity<Child>(b =>
        {
            b.HasOne<Parent>().WithMany().HasForeignKey(e => e.ParentGuid).IsRequired();
            b.HasKey("ParentGuid", "Id");
        });
    }
}

@kskalski
Copy link
Author

kskalski commented Dec 6, 2023

Ah, right, Key needs to implement some more interfaces:
System.Collections.Generic.IEnumerable<byte>, System.Collections.IEnumerable, IEquatable<Key>

Here is a complete project that reproduces bug for me:
efbug.tar.gz

ajcvickers added a commit that referenced this issue Dec 8, 2023
…t type as principal properties

Fixes #32411

The issue here is that when a value converter is applied to a principal property, then that converter is used by the dependent properties unless something else is explicitly configured. However, this meant that when the PK has a converter but the FK does not, then the FK can get configured as a primitive collection, since it doesn't have a converter preventing this.

The fix is to add a convention that sets the element type for dependent properties to match that for principal properties.
@ajcvickers ajcvickers self-assigned this Dec 8, 2023
@ajcvickers ajcvickers added this to the 8.0.x milestone Dec 8, 2023
ajcvickers added a commit that referenced this issue Dec 9, 2023
…t type as principal properties

Fixes #32411

The issue here is that when a value converter is applied to a principal property, then that converter is used by the dependent properties unless something else is explicitly configured. However, this meant that when the PK has a converter but the FK does not, then the FK can get configured as a primitive collection, since it doesn't have a converter preventing this.

The fix is to add a convention that sets the element type for dependent properties to match that for principal properties.
ajcvickers added a commit that referenced this issue Dec 9, 2023
…t type as principal properties (#32560)

* Ensure that, by convention, dependent properties have the same element type as principal properties

Fixes #32411

The issue here is that when a value converter is applied to a principal property, then that converter is used by the dependent properties unless something else is explicitly configured. However, this meant that when the PK has a converter but the FK does not, then the FK can get configured as a primitive collection, since it doesn't have a converter preventing this.

The fix is to add a convention that sets the element type for dependent properties to match that for principal properties.

* Update based on review
@ajcvickers ajcvickers reopened this Dec 9, 2023
ajcvickers added a commit that referenced this issue Dec 10, 2023
…t type as principal properties (#32560)

* Ensure that, by convention, dependent properties have the same element type as principal properties

Fixes #32411

The issue here is that when a value converter is applied to a principal property, then that converter is used by the dependent properties unless something else is explicitly configured. However, this meant that when the PK has a converter but the FK does not, then the FK can get configured as a primitive collection, since it doesn't have a converter preventing this.

The fix is to add a convention that sets the element type for dependent properties to match that for principal properties.

* Update based on review
@ajcvickers ajcvickers added closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. Servicing-approved and removed Servicing-consider labels Dec 10, 2023
@ajcvickers ajcvickers removed this from the 8.0.x milestone Dec 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-model-building closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported regression Servicing-approved type-bug
Projects
None yet
5 participants