My question here is if I should place a RowVersion [TimeStamp] property in every
entity in my domain model.
For Example: I have an Order class and an OrderDetails “navigation, reference” property,
should I use a RowVersion property for both entities, or is it enough to the parent object?
These classes are pocos meant to be used with Entity Framework Code First approach.
Thank you.
The answer, as often, is “it depends”.
Since it will almost always be possible to have an
Orderwithout anyOrderDetails, you’re right that the parent object should have aRowVersionproperty.Is it possible to modify an
OrderDetailwithout also modifying theOrder? Should it be?If it isn’t possible and shouldn’t be, a
RowVersionproperty at the detail level doesn’t add anything. You already catch all possible modifications by checking theOrder‘sRowVersion. In that case, only add the property at the top level, and stop reading here.Otherwise, if two independent contexts load the same order and details, both modify a different
OrderDetail, and both try to save, do you want to treat this as a conflict? In some cases, this makes sense. In other cases, it doesn’t. To treat it as a conflict, the simplest solution is to actually mark theOrderas modified too if it is unchanged (usingObjectStateEntry.SetModified, notObjectStateEntry.ChangeState). EF will check and cause an update to theOrder‘sRowVersionproperty, and complain if anyone else made any modifications.If you do want to allow two independent contexts to modify two different
OrderDetails of the sameOrder, yes, you need aRowVersionattribute at the detail level.That said: if you load an
Orderand itsOrderDetails into the same context, modify anOrderDetail, and save your changes, Entity Framework may also check and update theOrder‘sRowVersion, even if you don’t actually change theOrder, causing bogus concurrency exceptions. This has been labelled a bug, and a hotfix is available, or you can install .NET Framework 4.5 (currently available in release candidate form), which fixes it even if your application uses .NET 4.0.