简体   繁体   中英

Entity Framework not saving all changes made to tables

I'm trying to save data to two different tables in one method. The first db.SaveChanges() call, updates the data correctly but the second time I call SaveChanges, it doesn't do anything, even when I clearly change the data.

PhaseStatus truckPhase = db.PhaseStatus.Where(x => x.TruckId == truckId).FirstOrDefault();
RTrucks truck = db.RTrucks.Where(x => x.Id == truckId).FirstOrDefault();

using (var ContextTransaction = db.Database.BeginTransaction())
{
    db.PhaseStatus.Attach(truckPhase);
    var entryPS = db.Entry(truckPhase);
    entryPS.State = EntityState.Modified;
    db.SaveChanges(); //Success

    if (truckPhase.Phase.PhaseName == "Not Started")
        truck.Status = "Quoted";
    else truck.Status = "Active";

    db.RTrucks.Attach(truck);
    var entryRT = db.Entry(truck);
    entryPS.State = EntityState.Modified;
    db.SaveChanges(); //Fails with no errors
    ContextTransaction.Commit();
}

Does anyone have any idea why EF6 would do this? Am I doing something wrong?

In your second block change:

entryPS.State = EntityState.Modified;

to

entryRT.State = EntityState.Modified;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM