Hi I am using a unit of work pattern on an EF4 context.
The context is being passed across a few methods that all do work on it and then when done I calling save changes.
When I peform a check later on on the state of the object it is the old values not new. yet if i were to call save changes at that point the state I would expect to be seen on the context is saved to the database.
essentially the call stack would be
Context ctx;
UpdateItems(ctx);
CheckItemsValid(ctx);
DoSomehtingElse(ctx); //Saving changes here.
Now the CheckItemsValid is failing as it cant see the changes made in UpdateItems, yet it is the same context.
If I wrap the whole thing in a TransactionScope and call SaveChanges before calling check items everything works that seems a bit of a sledgehammer to crack a nut though.
I have tried the following after updating the items in the update items method.
ApplyCurrentValues();
Refresh(ClientWins);
AcceptAllChanges();
Has any one seen this issue before or do i have to just transact the whole thing and call saves changes mid flow?
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();
}
is the base of it the processvehicle call checks that SetAllRepairsOfTypeToComplete was successful
Set all repairs to complete simply updates an bit from true to false on the Repairs list that is on the context ctx.EFRepairs.
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;
}
The completedMechrepairsCount is always 0 even though they were updated in SetAllRepairsOfTypeToComplete on the same context.
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.