我现在面临一个让我疯狂几天的问题,希望有人能帮助我。这里是 ;
我正在将 EF4 与 oracle 数据库一起使用,将 devart 的 dotConnect 用于 oracle 作为提供者。我有 wcf 服务方法,它在下面调用 DeleteCabinet 方法;
public void DeleteCabinet(string pRID)
{
using(TransactionScope tranScope = new TransactionScope())
{
DBUtils.DeleteCabinetAndShelves(pRecordId);
//throw exception to test record not deleted
throw new Exception("xxx something has happened test xxx");
tranScope.Complete();
}
}
DBUtils.DeleteCabinetAndShelves 如下所示;
public void DeleteCabinetAndShelves(string pRecordId)
{
using(var context = new EdrmEntities())
{
var cabinet = context.Cabinets.Include("Shelves").Single(p => p.RID == pCabinetRID);
//mark all cabinet shelves for deletion
if (cabinet.Shelves != null)
{
foreach (var tempShelf in cabinet.Shelves.ToList())
{
context.DeleteObject(tempShelf);
}
}
//mark cabinet for deletion
context.DeleteObject(cabinet);
//save
context.SaveChanges();
}
}
当我从我的测试项目中调用 DeleteCabinet 时,不是 wcf 调用而是直接方法调用,它工作正常。它抛出异常,事务被回滚。因此,没有按预期从数据库中删除记录
问题是,当我从客户端调用服务方法(调用 DeleteCabinet)时,抛出异常,但记录已从 db 中删除。交易不回滚!
似乎调用 wcf 方法不会回滚事务,但这似乎很疯狂(至少对我而言),有人知道这可能发生的原因吗?
提前致谢