-
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
SaveChanges fails for owned entity when owner has PK with default values #23730
Comments
@smitpatel Regression? |
Did not try on older release. Ran into this when writing test in main branch. |
Confirmed regression from 3.1. |
Marking for re-triage. This only happens when the key value is not configured as generated, but no key value is set. This is a pseudo-negative case. That is, the first entity saved like this in 3.1 would have succeeded because the key value of zero is not taken yet. The second entity saved will cause a primary key violation. We should still look at either making this work for the first case, or more likely throw a better exception. But I don't think we need to patch. |
I ran into this problem when I try to add and save an entity with self relation |
…, non-generated, value Fixes #23730
@tosatoandrea and others who have thumbs-uped this issue. Are you sure you are running into this problem and not something different that generates the same error message? This particular issue only happens when you try to save entities that are configured not to have store-generated keys and yet do not have a key value set. This exception happens the first time you do this, but even if we fix this exception you will still get an error on the next entity saved, since it will have a conflicting key value. Therefore, it doesn't seem like fixing this will enable any meaningful scenarios. If you are hitting this, then can you explain what the scenario is so that we can better understand the value of making the fix? |
Note from triage: fix in 6.0 for now; hold off on patching until we get a confirmed customer report that isn't doing something that will always fail later. |
@ajcvickers - I think I've hit up against this bug in a real-world case that that won't always fail later. I'm converting a 3.1 to 5.0 application. This was working perfectly under 3.1 for a long time. I have a value object defined as:
This value object is part of an owns-many relationship, defined as
Upon migration of EF5, some of our tests fail with: In our value object, DocumentIndex = 0 and Type = AgreementSectionType.CoverPage are both valid values, however, because these also happen to coincide with the C# default values for the int and our enum, we hit up against this bug. If I change my code so that DocumentIndex starts as one, and my enum is defined as:
then my test-suite passes. - This work-around feels a little distasteful, as 'None' is not really a valid value in my domain. |
… not-set, non-generated, value Port of 6.0 fix #23875 to 5.0 release. Fixes #23730 **Description** An exception is thrown when no key value is set for a non-generated key of an owned type. Normally this is a negative case since non-generated key values must be explicitly set. However, this can works when the non-generated value is part of a composite key for which other parts of the key are generated. In this case, the non-generated part can have the same default value for multiple inserts without violating the primary key constraint. **Customer Impact** This is a regression for the case described above. There is no reasonable workaround. (We already fixed this for EF Core 6.0, but decided not to patch since it seemed to be a regression only in a negative case. Since then other customers have reported the issue and one customer outlined the scenario above where it is a regression in working code.) **How found** Reported by multiple customers. **Test coverage** Test coverage for this case has been added in this PR. **Regression?** Yes, from EF Core 3.1. **Risk** Low. The fix is already in EF Core 6.0 and is targetted to this case. Also quirked.
given 5.0.4 is released , should this be closed? |
… not-set, non-generated, value (#24178) * Don't leave unknown FK values when principal is known but has not-set, non-generated, value (#23875) Fixes #23730 * [5.0.x] Don't leave unknown FK values when principal is known but has not-set, non-generated, value Port of 6.0 fix #23875 to 5.0 release. Fixes #23730 **Description** An exception is thrown when no key value is set for a non-generated key of an owned type. Normally this is a negative case since non-generated key values must be explicitly set. However, this can works when the non-generated value is part of a composite key for which other parts of the key are generated. In this case, the non-generated part can have the same default value for multiple inserts without violating the primary key constraint. **Customer Impact** This is a regression for the case described above. There is no reasonable workaround. (We already fixed this for EF Core 6.0, but decided not to patch since it seemed to be a regression only in a negative case. Since then other customers have reported the issue and one customer outlined the scenario above where it is a regression in working code.) **How found** Reported by multiple customers. **Test coverage** Test coverage for this case has been added in this PR. **Regression?** Yes, from EF Core 3.1. **Risk** Low. The fix is already in EF Core 6.0 and is targetted to this case. Also quirked.
Facing this in 5.0.9. Tried all the versions till 5.0.5. Faced the same issue in all versions. This seems not to be fixed. Please consider reopening this and fixing it in 5.x. |
@Sayan751 Please open a new issue and attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate. |
I have found this issue trying to solve a problem in 6.0. My solution is to replace this with |
Adding only Blog works fine. Adding owned type with blog throws error
Happens for SqlServer/InMemory (provider agnostic.
The text was updated successfully, but these errors were encountered: