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.72k stars 3.17k forks source link

Stop combining delete and insert pairs into updates #30705

Open ajcvickers opened 1 year ago

ajcvickers commented 1 year ago

When two entity types share the same table, then deleting an instance and inserting a new instance into the same row is combined into an update for the row. This makes sense for table sharing, but we also started doing this for entities mapped to different tables. This causes problems when:

Therefore, we should stop doing this except where needed for table sharing.

roji commented 1 year ago

I'm not sure what we decided any more, but assuming we support optional dependents for table sharing (do we?), would it still make sense to go through the motions of doing delete and insert like we'd do for non-table-sharing? Basically I'd expect Remove/Add to perform exactly the same regardless of whether there's a SaveChanges in between - except where doing that would raise an exception (i.e. non-optional table sharing).

ajcvickers commented 1 year ago

But then we would have to also delete the other entities sharing the table, and re-create those, which would have different side effects.

roji commented 1 year ago

But I'd assume that if we support optional table sharing, it should already be possible to delete the dependent without deleting the principal no? And if so, there's no reason to merge the delete/add for that case...

ajcvickers commented 1 year ago

We can discuss.

ajcvickers commented 5 months ago

See also the issue described here: #33653

AndriySvyryd commented 1 month ago

https://github.com/dotnet/efcore/issues/1699 needs to be implemented before this, otherwise it would break the case where there's a relationship cycle in the database and all involved entities are replaced at the same time (already covered in tests).

Related: #33750 where we need to do the reverse