Skip to content

Support non-many-to-many skip navigations #21673

Description

@ajcvickers

Breaking out to track separately from #19003.

Activity

  1. added this to the Backlog milestone on Jul 17, 2020
  2. eraffel-MDSol commented on Aug 13, 2020

    @eraffel-MDSol

    I have a scenario that I think will require this, but want to confirm:
    Assuming TPT, given the following model, if I wanted to have the relationship between the derived Child and Parent without having to store ParentId on the Child table, would that be possible via skip-navigation, given that I'd be able to get to the Parent entity's Id from the ChildBase's BaseParentId property. Let me know if none of that made sense.

    public abstract class ParentBase
        {
            public Guid Id { get; set; }
        }
    
        public class Parent : ParentBase
        {
        }
    
        public abstract class ChildBase
        {
            public Guid Id { get; set; }
            public ParentBase BaseParent { get; set; }
            public Guid BaseParentId { get; set; }
        }
    
        public class Child : ChildBase
        {
            public Parent Parent { get; set; }
        }
  3. AndriySvyryd commented on Aug 14, 2020

    @AndriySvyryd
    Member

    @eraffel-MDSol A regular relationship will work for this case as Child is also a ChildBase and would inherit BaseParentId

  4. eraffel-MDSol commented on Aug 14, 2020

    @eraffel-MDSol

    I was thinking that since using TPT, BaseParentId is on a different table, it'd be an issue. Maybe I'm setting up the relationship wrong? Also, please let me know if I should open a new issue for this and I'll get rid of my comments here.

    If I put this in the OnModelCreating:

    modelBuilder.Entity<Child>()
        .HasOne(c => c.Parent)
        .WithMany()
        .HasPrincipalKey(parent => parent.Id)
        .HasForeignKey(c => c.BaseParentId);

    I get the following exception (sorry for the length, can move it to a gist if you want):

    System.ArgumentNullException: Value cannot be null. (Parameter 'key')
       at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
       at System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)
       at Microsoft.EntityFrameworkCore.Metadata.Conventions.SharedTableConvention.TryUniquifyForeignKeyNames(IConventionEntityType entityType, Dictionary`2 foreignKeys, StoreObjectIdentifier& storeObject, Int32 maxLength)
       at Microsoft.EntityFrameworkCore.Metadata.Conventions.SharedTableConvention.ProcessModelFinalizing(IConventionModelBuilder modelBuilder, IConventionContext`1 context)
       at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.ImmediateConventionScope.OnModelFinalizing(IConventionModelBuilder modelBuilder)
       at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.OnModelFinalizing(IConventionModelBuilder modelBuilder)
       at Microsoft.EntityFrameworkCore.Metadata.Internal.Model.FinalizeModel()
       at Microsoft.EntityFrameworkCore.ModelBuilder.FinalizeModel()
       at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, ModelDependencies modelDependencies)
       at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext context, IConventionSetBuilder conventionSetBuilder, ModelDependencies modelDependencies)
       at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()
       at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()
       at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.<>c.<TryAddCoreServices>b__7_3(IServiceProvider p)
       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.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass1_0.<RealizeService>b__0(ServiceProviderEngineScope scope)
       at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.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_InternalServiceProvider()
       at Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.get_Instance()
       at Microsoft.EntityFrameworkCore.Infrastructure.Internal.InfrastructureExtensions.GetService[TService](IInfrastructure`1 accessor)
       at Microsoft.EntityFrameworkCore.Infrastructure.AccessorExtensions.GetService[TService](IInfrastructure`1 accessor)
       at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func`1 factory)
       at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
       at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace)
       at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace)
       at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
       at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
       at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
    
  5. AndriySvyryd commented on Aug 14, 2020

    @AndriySvyryd
    Member

    @eraffel-MDSol That's just a bug. It should be fixed by acd5a1f, so try the latest daily build and if it still fails open a new issue.

  6. eraffel-MDSol commented on Aug 14, 2020

    @eraffel-MDSol

    I wanna make sure my model is clear, because I'm not actually convinced what I'm trying to do is or would be supported. I'm trying to map the relationship I've circled in the below image. Yes, what you said is correct, but I'm not sure how we'd get from the Child to the Parent. I know the Parent and ParentBase share the same key, and that Child has access to that key via ChildBase, but, the modeled relationship is from ChildBase to ParentBase, not to Parent. So for one, you wouldn't be able to have an actual foreign key relationship.

    I'm basically trying to use ChildBase as a join-table between Child and Parent, but since there's no relationship that points to Parent, idk how to map that.

    image

  7. AndriySvyryd commented on Aug 14, 2020

    @AndriySvyryd
    Member

    @eraffel-MDSol Parent is derived from ParentBase, so an FK to Parent can point either at ParentBase.Id or Parent.Id, from modeling perspective they are equivalent. The configuration you posted above should work.

  8. ajcvickers commented on Nov 3, 2020

    @ajcvickers
    ContributorAuthor

    See also lazy-loading: #19969

  9. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions