dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.64k stars 3.15k forks source link

Updates cannot delete navigations with shadow foreign keys in case of explicit tracking #34503

Open z505985755 opened 3 weeks ago

z505985755 commented 3 weeks ago

for example, the current data is a factory with two areas. When using explicit-tracking to transfer this factory, there is one area under this factory. The purpose is to modify the area relationship of the factory so that this factory contains only one area, but in fact the factory area relationship is not modified. This factory still has two area

roji commented 3 weeks ago

This issue is lacking enough information for us to be able to fully understand what is happening. Please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.

z505985755 commented 3 weeks ago

This issue is lacking enough information for us to be able to fully understand what is happening. Please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.

EFTest.zip I've posted a fully working, minimal console program that shows the problem, please check it out

ajcvickers commented 2 weeks ago

@z505985755 This is by-design--see Explicitly Tracking Entities in the docs. Specifically, there is no way to know that the navigation is null because it was loaded and then set to null, as opposed to just being null because it isn't loaded. The documentation contains various approaches you can take to handle this.

z505985755 commented 2 weeks ago

@z505985755 This is by-design--see Explicitly Tracking Entities in the docs. Specifically, there is no way to know that the navigation is null because it was loaded and then set to null, as opposed to just being null because it isn't loaded. The documentation contains various approaches you can take to handle this.

@ajcvickers You are right, I understand now. Maybe there should be a convention to indicate that the relationship is deleted, for example, when updating, if the navigation id is negative, it means that the navigation relationship is deleted, or there is another better way.