dotnet / dotnet/EntityFramework.Docs

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

Open
#2,348 0 comments 0 reactions 0 assignees View on GitHub
area-change-tracking
Dominant language
Mermaid
Stars
1.7k
Forks
2k
Avg merge
7d 23h
Merged PRs (30d)
16

Description

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?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.