2

您好我在 EF4 上下文中使用工作单元模式。

上下文正在通过几个方法传递,所有方法都在它上面工作,然后在完成后我调用保存更改。

当我稍后对对象的状态进行检查时,它是旧值而不是新值。但是,如果我此时调用保存更改,我希望在上下文中看到的状态将保存到数据库中。

本质上调用堆栈将是

Context ctx;

UpdateItems(ctx);
CheckItemsValid(ctx);
DoSomehtingElse(ctx); //Saving changes here.

现在CheckItemsValid失败了,因为它看不到在 中所做的更改UpdateItems,但它是相同的上下文。

如果我将整个事情包装在 TransactionScope 中并在调用检查项目之前调用 SaveChanges,那么一切正常,但这似乎有点像大锤来破解坚果。

在更新项目方法中更新项目后,我尝试了以下操作。

ApplyCurrentValues();
Refresh(ClientWins);
AcceptAllChanges();

以前有没有人看到过这个问题,还是我必须处理整个事情并在流程中调用保存更改?

    using (EntitiesContext ctx = EntityFactory.GetEntitiesContext())
        {

            Repairs.RepairManager repman = new Repairs.RepairManager();

            repman.SetAllRepairsOfTypeToComplete(vehicle.VehicleId, RepairTypes.BodyShop, vehicle.SiteVisitId.Value, technicianId, ctx);


            SchemeManager sm = new SchemeManager();
            result = sm.ProcessVehicle(AutoMapper.Mapper.Map<EFVehicle, AbstractVehicle>(ctx.EFVehicles.Single(p => p.VehicleID == vehicle.VehicleId), new Vehicle()), ref intrules, userId, ctx, false);


            if (result == true)
            {
                ctx.SaveChanges();         
            }

是流程车辆调用检查 SetAllRepairsOfTypeToComplete 是否成功的基础

将所有修复设置为完成只需将上下文 ctx.EFRepairs 上的修复列表中的某个位从 true 更新为 false。

  var cancelledrepairs = ((EntitiesContext)Context).EFRepairs.Where(p => p.VehicleID == vehicle.VehicleId && p.VehicleSiteVisitID == vehicle.SiteVisitId
            && p.RepairTypeID == (int)RepairTypes.BodyShop && p.RepairStatusID == (int)RepairStatusEnum.Cancelled).Count();

        var completedMechrepairs = ((EntitiesContext)Context).EFRepairs.Where(p => p.VehicleID == vehicle.VehicleId && p.VehicleSiteVisitID == vehicle.SiteVisitId
            && p.RepairTypeID == (int)RepairTypes.BodyShop && p.RepairStatusID == (int)RepairStatusEnum.Completed).Count();

        var CurrentMechanicalRepairs = ((EntitiesContext)Context).EFRepairs.Where(p=> p.VehicleID == vehicle.VehicleId && p.VehicleSiteVisitID == vehicle.SiteVisitId
            && p.RepairTypeID == (int)RepairTypes.BodyShop).Count();

        if ((completedMechrepairs + cancelledrepairs) == CurrentMechanicalRepairs)
            return true;
        else
        {
            failureMessage = "Not all mechanical repairs for this vehicle are complete";
            return false;
        }

CompleteMechrepairsCount 始终为 0,即使它们在同一上下文中的 SetAllRepairsOfTypeToComplete 中进行了更新。

4

1 回答 1

0

This should work in principle. I even created a local test that mimics this behavior and it worked for me. I think that the most likely causes are: (any/all)

1) Your SetAllRepairsOfTypeToComplete method isn't updating the object(s) you think it is.

-or-

2) Your query to get the completedMechrepairs count isn't picking up the correct object(s).

Try doing some debugging where your EFRepair object has a unique ID that you can use to see if it gets updated. Then get a List back from your completedMechrepairs check and see if that EFRepair object with that ID is in the list.

于 2012-12-13T17:56:44.437 回答