-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Improve exception message for underconfigured 1-to-1 relationships #8857
Comments
@GiorgioG You will need to configure the two relationships using the APIs described in the documentation link included in the exception message. |
@ajcvickers my model does not have corresponding FK Id fields, so it’s not possible. Why does it work for one property but not two, and why does it only appear to fail when I’m using a base class. This seems like a bug and not a user error. |
@GiorgioG Agreed that the documentation doesn't look clear on this. We'll try to resolve that. You will need to do something like: modelBuilder.Entity<Model>()
.HasOne(e => e.UserCreatedBy)
.WithMany();
modelBuilder.Entity<Model>()
.HasOne(e => e.UserLastModifiedBy)
.WithMany(); I think that's all you should need, assuming you want one-to-many relationships and you want to continue using shadow-state FKs. |
@ajcvickers Thank you! - so much for not being a user error ;) |
Re-opening to keep the docs issue on the radar. |
Assigning to @AndriySvyryd to change the exception message. |
When a base entity class has more than one nav property of the same type, Add-Migrations will fail with the following error:
Exception message:
System.InvalidOperationException: The child/dependent side could not be determined for the one-to-one relationship that was detected between 'User.UserCreatedBy' and 'User.UserLastModifiedBy'. To identify the child/dependent side of the relationship, configure the foreign key property. See http://go.microsoft.com/fwlink/?LinkId=724062 for more details.
Stack trace:
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.RelationshipValidationConvention.Apply(InternalModelBuilder modelBuilder)
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.OnModelBuilt(InternalModelBuilder modelBuilder)
at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)
at System.Collections.Concurrent.ConcurrentDictionary
2.GetOrAdd(TKey key, Func
2 valueFactory)at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()
at Microsoft.EntityFrameworkCore.Internal.LazyRef
1.get_Value() at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass16_0.<RealizeService>b__0(ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitTransient(TransientCallSite transientCallSite, ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitTransient(TransientCallSite transientCallSite, ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass16_0.<RealizeService>b__0(ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider) at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0
1.b__0()at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Steps to reproduce
This only seems to occur when there two (or at least two, haven't tried more) properties of the same type on a base entity class that is then inherited. I would expect to be able to express my intent in some way in DbContext.OnModelCreating to help EF understand what I want if this is not deemed to be a bug in its current behavior.
Further technical details
EF Core version: 1.1.2
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Win10
IDE: VS2017 latest update as of 6/14/2017
See attached solution: HD.zip
The text was updated successfully, but these errors were encountered: