Reputation: 8636
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.
Upvotes: 3
Views: 1248
Reputation:
The answer, as often, is "it depends".
Since it will almost always be possible to have an Order
without any OrderDetail
s, you're right that the parent object should have a RowVersion
property.
Is it possible to modify an OrderDetail
without also modifying the Order
? Should it be?
If it isn't possible and shouldn't be, a RowVersion
property at the detail level doesn't add anything. You already catch all possible modifications by checking the Order
's RowVersion
. 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 the Order
as modified too if it is unchanged (using ObjectStateEntry.SetModified
, not ObjectStateEntry.ChangeState
). EF will check and cause an update to the Order
's RowVersion
property, and complain if anyone else made any modifications.
If you do want to allow two independent contexts to modify two different OrderDetail
s of the same Order
, yes, you need a RowVersion
attribute at the detail level.
That said: if you load an Order
and its OrderDetail
s into the same context, modify an OrderDetail
, and save your changes, Entity Framework may also check and update the Order
's RowVersion
, even if you don't actually change the Order
, 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.
Upvotes: 4