-
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
Changing a DbSet<> property name can create incorrect TPH foreign keys in model #16762
Comments
@LeroyK Does this also fail for you on 2.2.6? @AndriySvyryd Repros for me on currently nightlies, but I'm not entirely sure what the expectation is in this case. |
My thoughts: 2nd one is correct. Pet.PetOwner navigation is defined on base class which is part of model. And inverse navigations (Dogs/Cats) points to derived type. We cannot pair those up. Derived types cannot have same navigation as base type unless it is ignored on base type and added in derived type. Without paired up navigations, you would get what is in configuration 2. Error is also valid bit difficult to understand. Trying to configure Does it work if you ignore the navigation in base class and then configure in derived types? |
No, this started happening after switching to 3.0. It worked fine in 2.2.6. |
I would say the first one is correct for the provider model, but not for the entity model. In OnModelCreating I am defining the navigations for
This is actually what I want, but since the navigation is automatically created by EF Core when the name is I actually got it to work by manually removing the foreign keys (not ideal):
In any case, model creation should be deterministic and not be affected by naming of (probably implicitly order of?) |
…targeting the derived type. Remove ambiguous navigations when the target type is ignored or a base type is set that has the navigation ignored. Remove ignored properties on the derived entity types when the configuration source of the base property is changed. Throw correct exception when the collection navigation type doesn't match the dependent entity type. Fixes #16762
…targeting the derived type. Remove ambiguous navigations when the target type is ignored or a base type is set that has the navigation ignored. Throw correct exception when the collection navigation type doesn't match the dependent entity type. Fixes #16762
…targeting the derived type. Remove ambiguous navigations when the target type is ignored or a base type is set that has the navigation ignored. Throw correct exception when the collection navigation type doesn't match the dependent entity type. Fixes #16762
Changing a
DbSet<>
property name can result in incorrect TPH foreign keys in the model.Just run the example code below with the following instructions.
The referencing foreign keys are captured at the first line of
OnModelCreating
(set a breakpoint), before any custom model configuration is done.When running the code with the
DbSet<Packs>
named asPacks"
everything runs fine.OnModelCreating
succeeds and the resulting referencing foreign keys onPetOwner
are (as expected):Now if we rename
Packs
toAPacks
, the referencing foreign keys onPetOwner
are:This is incorrect, foreign key [1] should not have been created. Furthermore,
OnModelCreating
now fails with the exception below.Steps to reproduce
Further technical details
EF Core version: 3.0.0-preview7.19362.6
Database Provider:Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10 1903
IDE: Visual Studio 2019 16.3.0 Preview 1
The text was updated successfully, but these errors were encountered: