Entity Framework not saving modified children

The problem is, that EF is not aware of the changed DefaultCharges.

By setting the State of the DevelopmentType to EntityState.Modified, EF only knows that the object DevelopmentType has been changed. However, this means that EF will only update DevelopmentType but not it’s navigation properties.

A workaround – which isn’t best practice – would be to iterate over all DefaultCharge of the current DevelopmentType and set the entity state to EntityState.Modified.

Additionally I would recommend to attach the entity to the context first, and change the state afterwards.

EDIT after comment

As you are using DTOs I suppose you are transfering these objects either through different layers or different machines.

In this case I would recommend to use self tracking entities, because it is not possible to share one context. These entities additionally holds their current state (ie. new, updated, deleted etc). There are many tutorials on the net about self tracking entities.

e.g. MSDN – Working with Self-Tracking Entities

Leave a Comment