After a few nights of trial and error, I've been unable to find a way to update a collection of custom types in a Many To Many relationship with Trackable Entities.
I have a type Bar that can be assigned to a collection member of Foo (Foo.Bars). In the API consumer, users select zero-to-many Bar from a list which means Bars may be added of removed from the Foo.Bars collection.
This is in an MVC setting, which is why I'm using TrackableEntities to manage updating properties. The best I've managed is to get selected Bar added to the collection. If a Foo that already had Bars in the Foo.Bars collection is posted back with no changes to the .Bars collection, EF produces a Foreign key violation as the same Bar is added again. Removing a Bar from the list in the frontend and posting the collection back with the new desired state of the collection does not remove the desired Bar(s) when saved. I toyed with setting the TrackingState of these Bar to deleted or modified but it appears to be ignored which does make sense - the Bar entity is not being updated or deleted; it's the Foo.Bars property that should be changed.
Has anyone worked with TrackableEntities and been able to get EF Core 5 Many-To-Many navigation properties to be managed? There's no sample of it in the TrackableEntities/TrackableEntities.Core.Sample project. Apparently TrackableEntities had M-to-M support in 2015 but I haven't been able to find anyone talking about it since.
Assuming that your many-to-many is modelled like this:
You can call
clear()on a tracked instance ofFoo, and then re-add theBarinstances that you want assigned. I've found that avoids the constraint exception.