Skip to content

Mismatch in the line number of code snippet vs description #5358

@eldhoabe

Description

@eldhoabe

Type of issue

Typo

Description

// 1. Query the blog with the name `SomeBlog`. Since EF queries are tracking by default, the Blog is now tracked by EF's change tracker.
var blog = await context.Blogs.SingleAsync(b => b.Name == "SomeBlog");

// 2. Increase the rating of all blogs in the database by one. This executes immediately.
await context.Blogs.ExecuteUpdateAsync(setters => setters.SetProperty(b => b.Rating, b => b.Rating + 1));

// 3. Increase the rating of `SomeBlog` by two. This modifies the .NET `Rating` property and is not yet persisted to the database.
blog.Rating += 2;

// 4. Persist tracked changes to the database.
await context.SaveChangesAsync();

[Crucially, when ExecuteUpdate is invoked and all Blogs are updated in the database, EF's change tracker is not updated, and the tracked .NET instance still has its original rating value, from the point at which it was queried. Let's assume that the Blog's rating was originally 5; after the 3rd line executes, the rating in the database is now 6 (because of the ExecuteUpdate), whereas the rating in the tracked .NET instance is 7. When SaveChanges is called, EF detects that the new value 7 is different from the original value 5, and persists that change. The change performed by ExecuteUpdate is overwritten and not taken into account.

As a result, it is usually a good idea to avoid mixing both tracked SaveChanges modifications and untracked modifications via ExecuteUpdate/ExecuteDelete.]

Instead of 3rd line executes it should be 2nd line
If the third line then the value would be 7 instead of 6.

Page URL

https://learn.microsoft.com/en-us/ef/core/saving/execute-insert-update-delete

Content source URL

https://github.com/dotnet/EntityFramework.Docs/blob/main/entity-framework/core/saving/execute-insert-update-delete.md

Document Version Independent Id

67bf0d8e-d6e4-7fe0-b86c-3586a19cd84f

Platform Id

5bccfc10-c6e5-de2f-1c1f-398798e6424f

Article author

@roji

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions