Document 'Disconnected Entities' - The code example in the Handling Deletes section works unexpected

Open Beginner friendly
#2,348 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
68/100
Issue type
Documentation
Clarity
Clearly specified
Activity status
Stale
Tech stack
csharp
Domain
documentation

Research direction

Start in the documentation's “Handling Deletes” section and review the graph-diff example using integer primary keys. Verify the behavior with an aggregate containing multiple new posts, then update the wording or example to account for multiple entities whose IDs are zero. Done means the documented example no longer implies that all new posts compare as the same entity.

Written by the indexing model from the issue text.

Description

area-change-tracking

This issue tracker is for documentation

Hi,

In the section 'Handling Deletes' there is a code example for handling true deletes using a graph diff. However I noticed that (at least for me) it worked unexpected when u are using integer PK's. For example when you send an aggregate like blog with 3 new posts. the posts are new and so the Id = 0. The first post gets added like expected but the next ones are not because now there is already one with id=0 so those posts are not added.

This behavior is caused by the following lines:
`var existingPost = existingBlog.Posts
.FirstOrDefault(p => p.PostId == post.PostId);

        if (existingPost == null)
        {
            existingBlog.Posts.Add(post);
        }
        else
        {
            context.Entry(existingPost).CurrentValues.SetValues(post);
        }`

For me it works expected when I change it to:
var existingPost = existingBlog.Posts .FirstOrDefault(p => p.PostId == post.PostId && p.PostId != 0)

Perhaps this should be mentioned in the docs or the example updated?

Dominant language
Mermaid
Stars
1.7k
Forks
2k
Avg merge
7d 23h
Merged PRs (30d)
16

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/EntityFramework.Docs

All issues in dotnet/EntityFramework.Docs

Similar issues

More Documentation issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.