I am using C#, EF and SQL Server and have few tables with parent-child relationship. There are more than 2 but in this example I will use only 2 for simplicity.
Table Book
Id
DateModified
RowVersion
Table Page
Id
BookId - this is foreign key to Book.Id
RowVersion
When any page/pages are updates I need to update DateModified
in Book table.
We are using RowVersion
to keep track of changes, since it's unique value I want to always be able to get the latest changes for each book by simply doing something like
SELECT * FROM Page
WHERE RowVersion > Book.RowVersion
But in order to do this I need to make sure that RowVersion
in Book
table is ALWAYS updated before RowVersions
in Pages
.
In my EDM layer I currently have something like this:
class Page
{
void OnPageChanged() //this is hooked to OnPropertyChanged
{
this.Book.UpdateDateModified(DataTime.Now);
}
}
Doing this results in page being updated first and its RowVersion is updated first too after the transaction commits. Which is not what I need.
Question, if I will move OnPageChanged()
to be handling OnPropertyChanging
event instead - will this guarantee the consistency? Is it OnPropertyChanged
that dictates the order the updates happen in sql generated by EF? Any other suggestions for this case?