[英]Getting the modifed entities in OpenAccessContext in case on an Exception
I know we can have access to the new/modified/deleted entities in OpenAccessContext by the following commands. 我知道我们可以通过以下命令访问OpenAccessContext中新的/修改的/删除的实体。
dbContext.GetChanges().GetInserts<object>() dbContext.GetChanges().GetUpdates<object>() dbContext.GetChanges().GetDeletes<object>()
If an execption occures for any reasons while perfoming SaveChanges(), I need to log those entities but they get cleared in case of exceptions. 如果在执行SaveChanges()时由于任何原因执行了执行,我需要记录这些实体,但是在出现异常的情况下会清除它们。
Is there anyway to get the entities out of OpenAccessContext in case on exceptions? 无论如何,是否存在将实体从OpenAccessContext中移出的情况,以防发生异常?
You could override the SaveChanges method like this: 您可以像这样覆盖SaveChanges方法:
public override void SaveChanges()
{
ContextChanges cruds = this.GetChanges;
IList<object> inserts = cruds.GetInserts<object>();
IList<object> updates = cruds.GetUpdates<object>();
IList<object> deletes = cruds.GetDeletes<object>();
try {
base.SaveChanges(ConcurrencyConflictsProcessingMode.AggregateAll);
} catch (Exception ex) {
// Retry or in your case log...
this.Refresh(RefreshMode.PreserveChanges, updates);
this.Delete(deletes);
this.Add(inserts);
Thread.Sleep(1000);
base.SaveChanges();
} finally {
this.FlushChanges(true);
}
}
When the savechanges failed, the changes are still accessible. 当保存更改失败时,更改仍可访问。 You could also log the exception while you are at it.
您也可以在处理异常时记录异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.