문제

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?

도움이 되었습니까?

해결책

I don't think this is possible. I cannot try it now but I have real doubts that you can compare timestamps (RowVersion) in Linq query. It is mapped to byte array and byte array is not comparable. The order of operations in the database is absolutely out of your control. You cannot base your logic on expected order in which the entities will be updated. You can do it only if your pages and book will be saved by two different SaveChanges calls. In such case you cannot use OnPropertyChanges at all and you must handle Book change manually before you make any change to your pages.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top