You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
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)
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.
@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.
Activity
eraffel-MDSol commentedon Aug 13, 2020
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
ChildandParentwithout having to storeParentIdon theChildtable, would that be possible via skip-navigation, given that I'd be able to get to the Parent entity's Id from the ChildBase'sBaseParentIdproperty. Let me know if none of that made sense.AndriySvyryd commentedon Aug 14, 2020
@eraffel-MDSol A regular relationship will work for this case as Child is also a ChildBase and would inherit BaseParentId
eraffel-MDSol commentedon Aug 14, 2020
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:
I get the following exception (sorry for the length, can move it to a gist if you want):
AndriySvyryd commentedon Aug 14, 2020
@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.
eraffel-MDSol commentedon Aug 14, 2020
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
Childto theParent. I know theParentandParentBaseshare the same key, and thatChildhas access to that key viaChildBase, but, the modeled relationship is fromChildBasetoParentBase, not toParent. So for one, you wouldn't be able to have an actual foreign key relationship.I'm basically trying to use
ChildBaseas a join-table betweenChildandParent, but since there's no relationship that points to Parent, idk how to map that.AndriySvyryd commentedon Aug 14, 2020
@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.
ajcvickers commentedon Nov 3, 2020
See also lazy-loading: #19969