我正在开发一个组件,其中使用了Entity Framework 4.0。
在特定情况下,我需要使用TransactionScope
。
try
{
using (TransactionScope t = new TransactionScope())
{
myEntity.MyObjectsOne.Add(new MyObjectOne());
myEntity.MyObjectsTwo.Add(new MyObjectTwo());
myEntity.SaveChanges();
throw new Exception();
t.Complete();
}
}
catch (Exception e)
{
// What should I do?
}
当事务失败并未完成时,myEntity仍将反映所做的更改,即添加了MyObjectOne
和MyObjectTwo
。
在这种情况下,为避免不一致性,最好采用什么样的最佳实践?