-
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
Set TPH discriminator value correctly when discriminator is part of the primary key #21206
Comments
Note for team: The reported exception is no longer thrown in EF Core 5.0. However, since the value is also on the dependent end of a relationship, we currently throw:
This might be fixable by changing the order of discriminator verses PK/FK initialization. |
Investigated this a bit more and it seems like the discriminator property is being incorrectly set to "ProviderContract". Explicitly setting to the correct value works: var partner = await context.Partners.FirstAsync();
var contract = new ProviderContract1
{
Partner = partner,
Details = "Provider Contract Details"
};
context.Add(contract);
context.Entry(contract).Property("ProviderId").CurrentValue = "prov1"; Marking this as a bug. |
After migrating from 3.1 to 5.0 Im seeing a similar issue, under different conditions e.g. data model. |
Also, fixes #21206. Let value generators indicate that they return stable values, and don't mark these values as Unknown. This stops values generated for discriminator properties from being marked as Unknown.
…d collection (#25652) * Detect unknown shadow primary key values when attempting to save owned collection Fixes #19856 The fundamental issue here is that the key for owned collections is, by default, formed from shadow properties. This means the values of these properties are lost when the entities in the collection are no longer tracked by a DbContext. When the entities are then later attached there is no way to get these values back. (Contrast this with owned non-collection entities, where the shadow key value can be synthesized from the owner key.) This in turn means that there is no way to identify these entities in the database, and there is therefore no way to update or delete them. This PR detects this situation and throws an exception with a (hopefully) helpful error message. The best way to deal with this is to add a CLR property for the key (which can be private) to the owned entity type. The key values are then preserved while the entities are not being tracked, and re-attaching the entities works correctly. We should document this. * Adding async code and tests. Also, fixes #21206. Let value generators indicate that they return stable values, and don't mark these values as Unknown. This stops values generated for discriminator properties from being marked as Unknown.
I found a problem when I try to use the discriminator of a TPH mapping as part of primary key.
The Model what I want to map
SQL schema
Table: [ProviderContracts]
Columns:
PK: [PartnerId], [ProviderId]
DbContext
Sample usage
When I run this program, the save changes will throw the following error:
The problem is that the EF tries to execute the following SQL command:
Workaround
The modified DbContext
When I made these changes on the mapping, the sample program works fine.
Further technical details
EF Core version: 3.1.3
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Framework 4.7.2
Operating system: Windows 10
IDE: Visual Studio 2019 16.6.1
The text was updated successfully, but these errors were encountered: