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

Improve exception message for underconfigured 1-to-1 relationships #8857

Closed
GiorgioG opened this issue Jun 14, 2017 · 6 comments
Closed

Improve exception message for underconfigured 1-to-1 relationships #8857

GiorgioG opened this issue Jun 14, 2017 · 6 comments
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Milestone

Comments

@GiorgioG
Copy link

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.ConcurrentDictionary2.GetOrAdd(TKey key, Func2 valueFactory)
at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()
at Microsoft.EntityFrameworkCore.Internal.LazyRef1.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_01.b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)

Steps to reproduce

  1. Open attached Solution (HD.sln) in VS2017 (15.2 26430.13)
  2. Restore NuGet Packages
  3. Open Package Manager Console
  4. Execute command 'Add-Migrations InitialMigration'
  5. Observe bug
  6. Remove either UserCreatedBy or UserLastModifiedBy property from \src\HD.Core\Data\Models\Model.cs
  7. Execute command 'Add-Migrations InitialMigration' - now it works.

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

@GiorgioG GiorgioG changed the title Add-Migrations (and EF runtime) fails when a base entity class has more than 1 nav property of the same type (using shadow properties for keys) Add-Migrations (and EF runtime) fails when a base entity class has more than 1 nav property of the same type (using shadow properties) Jun 14, 2017
@ajcvickers
Copy link
Member

@GiorgioG You will need to configure the two relationships using the APIs described in the documentation link included in the exception message.

@GiorgioG
Copy link
Author

@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.

@ajcvickers
Copy link
Member

@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.

@GiorgioG
Copy link
Author

@ajcvickers Thank you! - so much for not being a user error ;)

@ajcvickers
Copy link
Member

Re-opening to keep the docs issue on the radar.

@ajcvickers
Copy link
Member

Assigning to @AndriySvyryd to change the exception message.

@AndriySvyryd AndriySvyryd removed their assignment Jun 28, 2017
@AndriySvyryd AndriySvyryd added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jun 28, 2017
@divega divega added type-bug and removed type-bug labels Aug 11, 2017
@divega divega changed the title Add-Migrations (and EF runtime) fails when a base entity class has more than 1 nav property of the same type (using shadow properties) Improve exception message for underconfigured 1-to-1 relationships Aug 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Projects
None yet
Development

No branches or pull requests

4 participants