发生异常时,无法使用块回滚事务

时间:2016-08-22 12:47:54

标签: c# entity-framework transactions

如果在使用工作单元块中发生了一些异常,我无法回滚事务。

这是我的代码:

using(var scope = new UnitOfWork()) 
{
        var entities = employeeRepository.Search(x => x.IsActive == true && x.IsDeleted == false);
        DateTime latestDate = LastVersionDate(Type);
        switch (type)
        {
            case EmployeeReportType.New:
                entities = entities.Where(x => x.CreatedOn >= latestDate);
                break;
            case EmployeeReportType.Modified:
                entities = entities.Where(x => x.UpdatedOn >= latestDate);
                break;
            default:
                throw new NotImplementedException();
        }
        LogToDatabase(Type, userId); // Here I am logging Report Downloading Time.


        // Here exception is occurred.
        var list = entities.Any() ? entities.AsEnumerable().Select(x => new EmployeeReportModel(x)).ToList() : new List<EmployeeReportModel>();  

        return list;
}  // transaction successfully commit (Which is wrong because  exception is occurred) 

LogToDatabase方法在我的日志表中添加条目

LastVersionDate方法获取上次下载的报告日期

0 个答案:

没有答案