Sometimes it is necessary to take an action (e.g. modify another entity's persistent state) when certain conditions are true in another entity's OnValidate() method. When doing so, DO throws: System.InvalidOperationException : Collection was modified; enumeration operation may not execute. I'm assuming that it enumerates through the collection of modified entities and calls OnValidate on each entity. So if during this enumeration you modify another entity, it causes the collection to be modified. I guess you can avoid this by making such changes in OnSetFieldValue instead of OnValidate. However, this poses a problem when using DisconnectedState. Imagine the following scenario. I have an Order entity. Order has a status property of type enum, where the enum has 3 values (PendingConfirmation, Confirmed, Shipped). When Order.Status is set to shipped, I want to mark the corresponding Shipment entity's Shipment.ShippingDate = DateTime.Now. In the disconnected scenario, the user is presented with a UI for the Order entity that has a combo box for the Status property. The combo box has the three enum values. If the user accidentally selects "Shipped", but then changes it to "Confirmed", and saves... then the history of actions is going to be replayed "Online". So the model is going to receive a message for Order.Status == Shipped and then for Order.Status == Confirmed. In such a scenario, I don't want to mark Shipment.ShippingDate = DateTime.Now, because that should only happen when Order.Status == Shipped. In such a scenario it is difficult to accomplish this through OnSetFieldValue. It is much easier to do it through OnValidate. So, what do you suggest? Is it bad practice to modify other entities through OnValidate()? Or is this just some bug that can be easily fixed? This thread was imported from our support forum. The original discussion may contain more detailed answer. |