OptimisticConcurrencyException Does Not Work in Entity Framework In Certain Situations

前端 未结 4 1444
小鲜肉
小鲜肉 2020-12-24 02:59

UPDATE (2010-12-21): Completely rewrote this question based on tests that I\'ve been doing. Also, this used to be a POCO specific question, but it turns out that my

4条回答
  •  星月不相逢
    2020-12-24 03:18

    Here is another approach that is a bit more generic and fits in the data layer:

    // if any timestamps have changed, throw concurrency exception
    var changed = this.ChangeTracker.Entries<>()
        .Any(x => !x.CurrentValues.GetValue("Timestamp").SequenceEqual(
            x.OriginalValues.GetValue("Timestamp")));
    if (changed) throw new OptimisticConcurrencyException();
    this.SaveChanges();
    

    It just checks to see if the TimeStamp has changed and throws concurrency exception.

提交回复
热议问题