3

我正在使用 EF6 和 GraphDiff 2.0.1。当我保存过时的条目时,我想检查 DbUpdateConcurrencyException。当我在没有 UpdateGraph 的情况下执行此操作时:没问题,EF 会引发异常。

try
{
    var dbEntry = Context.Persons.SingleOrDefault(p => p.PersonId == 
        disconnectedEntry.PersonId);
    dbEntry.Name = disconnectedEntry.Name;
    Context.Entry(dbEntry).OriginalValues["RowVersion"] = disconnectedEntry.RowVersion;
    DbContext.SaveChanges();
}
catch (DbUpdateConcurrencyException ex)
{
    // Exception thrown as expected.
}

我使用此处描述的解决方案来实现这一点: EF not throwing DbUpdateConcurrencyException 尽管更新冲突

问题是:如果我在 EF 的 SaveChanges 之前使用 GraphDiff 的 UpdateGraph:UpdateGraph 会引发不完整的 DbUpdateConcurrencyException。实际上,异常不包含条目。

try
{
    DbContext.UpdateGraph(disconnectedEntry);
    DbContext.SaveChanges();
}
catch (DbUpdateConcurrencyException ex)
{
    // Exception thrown as expected. But ex is incomplete
}

我很惊讶 UpdateGraph 引发了这个异常。据我说,UpdateGraph 更新连接的条目,但不保存。因此,它应该什么都不做,让 EF 来完成这项工作。

有没有办法避免 UpdateGraph 引发异常。至少,如果它提出一个完整的,它可能会很棒。

4

0 回答 0