-
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
[Regression] Replacing PK to PK TPH dependent with different type fails #29789
Labels
area-save-changes
closed-fixed
The issue has been fixed and is/will be included in the release indicated by the issue milestone.
customer-reported
Servicing-approved
type-bug
Milestone
Comments
Jejuni
changed the title
[Regression] Derived Entites not updated correctly
[Regression] Derived Entites not updated correctly since v7
Dec 7, 2022
Note for triage: this is a consequence of making the discriminator read-only. The model is in the following state before
The update pipeline collapses the Delete and Add into an Update, but the discriminator value is not changed because it is marked as "AfterSave:Throw". @Jejuni Workaround is to configure the discriminator such that it is allowed to be modified: modelBuilder.Entity<EntityWithInheritance>()
.Property(EntityWithInheritanceConfig.DiscriminatorColumnName)
.Metadata
.SetAfterSaveBehavior(PropertySaveBehavior.Save); |
ajcvickers
changed the title
[Regression] Derived Entites not updated correctly since v7
[Regression] Replacing PK to PK TPH dependent with different type fails
Dec 16, 2022
This was referenced Dec 16, 2022
ajcvickers
added
the
closed-fixed
The issue has been fixed and is/will be included in the release indicated by the issue milestone.
label
Jan 5, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
area-save-changes
closed-fixed
The issue has been fixed and is/will be included in the release indicated by the issue milestone.
customer-reported
Servicing-approved
type-bug
Description
Assume we have 3 concrete and 1 abstract entity:
MainEntity
(concrete)EntityWithInheritance
(abstract)EntityWithInheritanceOne
(concrete, derives fromEntityWithInheritance
)EntityWithInheritanceTwo
(concrete, derives fromEntityWithInheritance
)The hierarchy between them is:
MainEntity
->EntityWithInheritance
configured as 1:1Assume we have an instance as follows:
MainEntity
->EntityWithInheritanceOne
saved in the database.The discriminator column correctly denotes
EntityWithInheritanceOne
.We now want to update
MainEntity
by replacing the existing property with one of typeEntityWithInheritanceTwo
.After calling
SaveChanges
the discriminator column still incorrectly has the OLD value ofEntityWithInheritanceOne
instead of the correct, new one.All other (potential) properties seem to have updated correctly.
This did NOT happen in any EfCore version prior to version 7.
Repro
The following code in an excerpt from the linked repo
You will need to replace the connection string in
MyContext
class to point to a valid SqlServer instance.Database and config:
And a program to test the problem:
Notice the exception being thrown when the incorrect type is returned.
Switch
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.0" />
to<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.10" />
or any other version prior to 7 and the code above works,NOTE: The configuration above is the most default setup we could think of. Adding a
MainEntityId
property toEntityWithInheritance
and then configuring that property as the foreign key to theMainEntity
table the code above also works. Probably because the FK and PK are now different. This is obviously undesirable, however, as it requires additional configuration and the PK is essentially useless.Include provider and version information
EF Core version:
Database provider: Microsoft.EntityFrameworkCore.SqlServer 7.0.0
Target framework: (e.g. .NET 7.0)
Operating system: Windows
IDE: (e.g. Visual Studio 2022 17.4)
The text was updated successfully, but these errors were encountered: