I have a detached object which contains a collection. I'm attaching it using this code
EntityEntry dbEntityEntry = _context.Entry<T>(entity);
dbEntityEntry.State = EntityState.Modified;
Detached object saves perfectly if I change most of the fields.. the issue is with a collection this object has. Changes on the collection are not persisted. I've seen the query executed and it onl touches the "main" table. All dependent relations are not updated
Is there any way to update the entire tree for a detached object?
Problem :
EF is not aware of the changed collection or about the navigation properties.By setting the State of the dbEntityEntry to EntityState.Modified, EF only knows that the object dbEntityEntry has been changed. This means that EF will only update dbEntityEntry but not it's navigation properties.
Solution :
You can iterate over all collection (or navigation properties) of the current dbEntityEntry and set the entity state to EntityState.Modified.
Another Solution : Please see that too.May be helped to you.
Working with Self-Tracking Entities
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With