Skip to content
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

Closed
ajcvickers opened this issue Nov 10, 2016 · 5 comments
Closed

DbContext: Stop special-casing the root entity on Attach #6990

ajcvickers opened this issue Nov 10, 2016 · 5 comments
Labels
breaking-change closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Milestone

Comments

@ajcvickers
Copy link
Member

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.

ajcvickers added a commit that referenced this issue Nov 10, 2016
Issue #6990

Makes Attach/Update more useful by properly using the value-generation semantics built into the model.
@rowanmiller rowanmiller added this to the 1.2.0 milestone Nov 14, 2016
ajcvickers added a commit that referenced this issue Nov 16, 2016
Issue #6990

Makes Attach/Update more useful by properly using the value-generation semantics built into the model.
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Nov 16, 2016
@ajcvickers ajcvickers changed the title Stop special-casing the root entity on Attach DbContext: Stop special-casing the root entity on Attach May 9, 2017
@JonPSmith
Copy link

JonPSmith commented Jun 25, 2017

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:

  • It didn't say in the title that Update has also been changed, although later in the announcement it talked about Update.
  • Also, I don't think your announcement defines the change as clearly as note at the top of this post, i.e. it doesn't say that it will set the root entity State to Added for default PK, or non-database generated PK.
  • Can I suggest that you look at the original issue Change default graph behavior of Add/Attach/etc #4424 which outlined how EF Core differs from EF6.x., and see if it needs editing (it isn't obvious to me what you would add, but you may have a better idea.
    I think the initial comment, written by Rowan Miller, is a very useful summary on why the changes were made. Julie Lerman refers to it in one of her articles, and I also refer to it in the book I am writing.

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 v2.0.0-preview-final I can see both Attach and Update have changed.

@bertstomphorst
Copy link

@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.

@ajcvickers
Copy link
Member Author

@bertstomphorst Please file a new issue and include a small, runnable project/solution or complete code listing that demonstrates the behavior you are seeing.

@bertstomphorst
Copy link

@ajcvickers I tried to make an isolated reproduction, but that failed. Will do further research and open a new issue when necessary.

@bertstomphorst
Copy link

@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.

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)

@ajcvickers ajcvickers modified the milestones: 2.0.0-preview1, 2.0.0 Oct 15, 2022
@ajcvickers ajcvickers removed their assignment Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Projects
None yet
Development

No branches or pull requests

4 participants