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

XML comments for DbContext.Attach unclear #14037

Closed
nphmuller opened this issue Nov 29, 2018 · 0 comments · Fixed by #15435
Closed

XML comments for DbContext.Attach unclear #14037

nphmuller opened this issue Nov 29, 2018 · 0 comments · Fixed by #15435
Labels
area-docs closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported good first issue This issue should be relatively straightforward to fix. help wanted This issue involves technologies where we are not experts. Expert help would be appreciated. type-bug
Milestone

Comments

@nphmuller
Copy link

nphmuller commented Nov 29, 2018

The current XML summary of DbContext.Attach:

Begins tracking the given entity in the Microsoft.EntityFrameworkCore.EntityState.Unchanged
state such that no operation will be performed when Microsoft.EntityFrameworkCore.DbContext.SaveChanges
is called.

A recursive search of the navigation properties will be performed to find reachable
entities that are not already being tracked by the context. These entities will
also begin to be tracked by the context. If a reachable entity has its primary
key value set then it will be tracked in the Microsoft.EntityFrameworkCore.EntityState.Unchanged
state. If the primary key value is not set then it will be tracked in the Microsoft.EntityFrameworkCore.EntityState.Added
state. An entity is considered to have its primary key value set if the primary
key property is set to anything other than the CLR default for the property type.

Use Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry.State to set the
state of only a single entity.

The first paragraph describes that the EntityState will always be set to Unchanged. But when the key is set it will actually be set to Added.

This behaviour is described in the second paragraph, but it's described in such a way that that only should be the case on navigation properties of the main entity. Apparently it's also the case for the main entity.

I think this behaviour is great, but the XML summary should specify that it also applies to the main entity.

Example:

var ctx = new MyContext();
var newOne = new MyEntity();
var newTwo = new MyEntity() { Id = 1 };
var newOneEntry = ctx.Attach(newOne);
var newTwoEntry = ctx.Attach(newTwo);

Console.WriteLine(newOneEntry.State); // Prints `Added`, although the docs describe it should be `Unchanged`.
Console.WriteLine(newTwoEntry.State); // Prints `Unchanged` as expected.

Further technical details

EF Core version: 2.1.4

@ajcvickers ajcvickers self-assigned this Dec 3, 2018
@ajcvickers ajcvickers added this to the 3.0.0 milestone Dec 3, 2018
@ajcvickers ajcvickers added area-docs help wanted This issue involves technologies where we are not experts. Expert help would be appreciated. customer-reported labels Dec 3, 2018
@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 Apr 21, 2019
ajcvickers added a commit that referenced this issue Apr 21, 2019
* Fixes #15294 - BoolToStringConverter should not check if falseValue or trueValue is empty
* Fixes #15266 - Small typo in resource file
* Fixes #14037 - XML comments for DbContext.Attach unclear
* Fixes #14001 - DbSet.Local does not automatically call DetectChanges
* Fixes #13210 - WithExtension suggests that 'this' is updated, as well as returning 'this'
ajcvickers added a commit that referenced this issue Apr 21, 2019
* Fixes #15294 - BoolToStringConverter should not check if falseValue or trueValue is empty
* Fixes #15266 - Small typo in resource file
* Fixes #14037 - XML comments for DbContext.Attach unclear
* Fixes #14001 - DbSet.Local does not automatically call DetectChanges
* Fixes #13210 - WithExtension suggests that 'this' is updated, as well as returning 'this'
ajcvickers added a commit that referenced this issue Apr 22, 2019
* Fixes #15294 - BoolToStringConverter should not check if falseValue or trueValue is empty
* Fixes #15266 - Small typo in resource file
* Fixes #14037 - XML comments for DbContext.Attach unclear
* Fixes #14001 - DbSet.Local does not automatically call DetectChanges
* Fixes #13210 - WithExtension suggests that 'this' is updated, as well as returning 'this'
ajcvickers added a commit that referenced this issue Apr 22, 2019
* Fixes #15294 - BoolToStringConverter should not check if falseValue or trueValue is empty
* Fixes #15266 - Small typo in resource file
* Fixes #14037 - XML comments for DbContext.Attach unclear
* Fixes #14001 - DbSet.Local does not automatically call DetectChanges
* Fixes #13210 - WithExtension suggests that 'this' is updated, as well as returning 'this'
ajcvickers added a commit that referenced this issue Apr 22, 2019
* Fixes #15294 - BoolToStringConverter should not check if falseValue or trueValue is empty
* Fixes #15266 - Small typo in resource file
* Fixes #14037 - XML comments for DbContext.Attach unclear
* Fixes #14001 - DbSet.Local does not automatically call DetectChanges
* Fixes #13210 - WithExtension suggests that 'this' is updated, as well as returning 'this'
@bricelam bricelam added the good first issue This issue should be relatively straightforward to fix. label May 31, 2019
@ajcvickers ajcvickers modified the milestones: 3.0.0, 3.0.0-preview6 Jun 5, 2019
@ajcvickers ajcvickers modified the milestones: 3.0.0-preview6, 3.0.0 Nov 11, 2019
@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
area-docs closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported good first issue This issue should be relatively straightforward to fix. help wanted This issue involves technologies where we are not experts. Expert help would be appreciated. type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants