efcore.pg icon indicating copy to clipboard operation
efcore.pg copied to clipboard

Two FK relationships for one to unique many table - NpgsqlModelValidator failure

Open Ryujose opened this issue 3 years ago • 1 comments

Version 7.0.4

Issue When trying to add two FK on a relationship of one to many to another unique table exception is throwed at Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure.Internal.NpgsqlModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger)

Constraints ALTER TABLE public.Table1 ADD CONSTRAINT fk_table1_property1_id FOREIGN KEY (property1_id) REFERENCES Table2(id) ON UPDATE RESTRICT ON DELETE RESTRICT

ALTER TABLE public.Table1 ADD CONSTRAINT fk_table1_property2_id FOREIGN KEY (property2_id) REFERENCES Table2(id) ON UPDATE RESTRICT ON DELETE RESTRICT

C# classes

1rst approach

Public class1
{
        public Class2 NavigationProperty1 { get; set; } = default!;

        public Class2 NavigationProperty2 { get; set; } = default!;
}
Public class2
{
       public ICollection<Class1> Class1Collection { get; private set; } = default!;
}
              builder
              .HasOne(a => a.NavigationProperty1)
              .WithMany(b => b.Class1Collection)
              .HasForeignKey("navigation_property1_id");

              builder
              .HasOne(a => a.NavigationProperty2)
              .WithMany(b => b.Class1Collection)
              .HasForeignKey("navigation_property2_id");

2nd approach

Public class1
{
        public Class2 NavigationProperty1 { get; set; } = default!;

        public Class2 NavigationProperty2 { get; set; } = default!;
}
Public class2
{
       public ICollection<Class1> NavigationProperty1Class1Collection { get; private set; } = default!;
       public ICollection<Class1> NavigationProperty2Class1Collection { get; private set; } = default!;
}
               builder
               .HasOne(a => a.NavigationProperty1)
               .WithMany(b => b.NavigationProperty1Class1Collection)
               .HasForeignKey("navigation_property1_id");

               builder
               .HasOne(a => a.NavigationProperty2)
               .WithMany(b => b.NavigationProperty2Class1Collection)
               .HasForeignKey("navigation_property2_id");

3rd approach (with combination of approach 1 and 2 for class2)

Public class1
{
        public Class2 NavigationProperty1 { get; set; } = default!;

        public Class2 NavigationProperty2 { get; set; } = default!;
}
Approach combination 1

``` Public class2 { public ICollection NavigationProperty1Class1Collection { get; private set; } = default!; public ICollection NavigationProperty2Class1Collection { get; private set; } = default!; } ```

OR
Approach combination 2

Public class2
{
       public ICollection<Class1> Class1Collection { get; private set; } = default!;
}

               builder
               .HasOne(a => a.NavigationProperty1)
               .WithMany(b => b.NavigationProperty1Class1Collection)
               .HasForeignKey("navigation_property1_id");

               builder
               .HasOne(a => a.NavigationProperty2)
               .WithMany()
               .HasForeignKey("navigation_property2_id");

Error message from exception

"Unable to determine the relationship represented by navigation 'Class1.NavigationProperty2' of type 'Class2'.

Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'."

I've already tried with the following points

  • Added two navigation properties ICollection on the "many" side to try to separate the behavior mapping validation.
  • Added the mapping with fluent API from the two sides at the same time.
  • Added the mapping with fluent API from only one side at a time.
  • Added id properties in aggregate model and tried all the the points with them.
  • Tried to set in fluent API the inversion property 

Already checked

  • Dbset is set on dbContext
  • Entity configurations are set in dbContext

StackTrace

Details

at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.ValidatePropertyMapping(IModel model, IDiagnosticsLogger1 logger)    at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.Validate(IModel model, IDiagnosticsLogger1 logger)    at Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelValidator.Validate(IModel model, IDiagnosticsLogger1 logger)    at Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure.Internal.NpgsqlModelValidator.Validate(IModel model, IDiagnosticsLogger1 logger)    at Microsoft.EntityFrameworkCore.Infrastructure.ModelRuntimeInitializer.Initialize(IModel model, Boolean designTime, IDiagnosticsLogger1 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.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context)    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.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.CallSiteVisitor2.VisitCallSite(ServiceCallSite callSite, TArgument argument)    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.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.CallSiteVisitor2.VisitCallSite(ServiceCallSite callSite, TArgument argument)    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.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.CallSiteVisitor2.VisitCallSite(ServiceCallSite callSite, TArgument argument)    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.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.CallSiteVisitor2.VisitCallSite(ServiceCallSite callSite, TArgument argument)    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.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.CallSiteVisitor2.VisitCallSite(ServiceCallSite callSite, TArgument argument)    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.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.b__0(ServiceProviderEngineScope scope)    at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType, 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.get_DbContextDependencies()    at Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Internal.IDbContextDependencies.get_StateManager()

Ryujose avatar May 03 '23 12:05 Ryujose

Please post this in the general EF repo (https://github.com/dotnet/efcore), it doesn't seem specific to Npgsql/PostgreSQL in any way.

roji avatar May 03 '23 15:05 roji