CareTaker22
CareTaker22

Reputation: 1310

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?

Upvotes: 0

Views: 431

Answers (1)

Stephen Brickner
Stephen Brickner

Reputation: 2602

In your second block change:

entryPS.State = EntityState.Modified;

to

entryRT.State = EntityState.Modified;

Upvotes: 3

Related Questions