简体   繁体   English

实体框架未保存对表所做的所有更改

[英]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. 第一次调用db.SaveChanges()可以正确更新数据,但是第二次调用SaveChanges则不会执行任何操作,即使我明显地更改了数据也是如此。

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? 有谁知道为什么EF6会这样做? Am I doing something wrong? 难道我做错了什么?

In your second block change: 在第二块更改中:

entryPS.State = EntityState.Modified;

to

entryRT.State = EntityState.Modified;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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