-
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
DbContext: Stop special-casing the root entity on Attach #6990
Comments
Issue #6990 Makes Attach/Update more useful by properly using the value-generation semantics built into the model.
Issue #6990 Makes Attach/Update more useful by properly using the value-generation semantics built into the model.
Hi @ajcvickers, I think this is a good change - it makes a lot of sense. Here is some feedback on the announcement. I saw your announcement, and I noted that:
PS. I did run a unit test around non-database generated primary keys (see This Unit Test, which is for v1.1.1). When I run the same test on |
@ajcvickers it seems that this fix doesn't work when the (nested) entities key is a DatabaseGenerated int, because the IsKeySet returns True in that scenario (and the key is set to a negative number). Just seen this on EF 2.2.3, and fixed the unwanted add by setting the State to Modified when key>0. |
@bertstomphorst Please file a new issue and include a small, runnable project/solution or complete code listing that demonstrates the behavior you are seeing. |
@ajcvickers I tried to make an isolated reproduction, but that failed. Will do further research and open a new issue when necessary. |
This is issue is fixed in 2.2.4 (it re-occurred in my productioncode with 2.2.3, reproducable, and is gone after updating to 2.2.4) |
Attach sets the state of all untracked entities in the reachable graph to Unchanged, unless the key for the entity has not been set and is using value generation. The same is true for Update/Modified. This would work great for many common cases of disconnected entities.
However, it doesn't work in many cases only because we special case the root entity and force its state to Unchanged even if the key is not set. This is so that you don't call Attach on a single entity and end up with it being Added, which was considered an unexpected behavior.
We should stop special-casing the root entity because this would make Attach more generally useful. It probably isn't even a breaking change because attaching an entity with a store generated key that does not have a key value set will throw either immediately or when a second entity does the same thing.
The text was updated successfully, but these errors were encountered: