我在使用 Entity Framework 4.0 时遇到问题。我正在尝试保存具有“段”对象集合的“治疗”对象。每当我尝试添加/编辑要添加 2 个或更多新段的处理对象时,都会收到以下错误:
对数据库的更改已成功提交,但在更新对象上下文时出错。ObjectContext 可能处于不一致的状态。内部异常消息:AcceptChanges 无法继续,因为对象的键值与 ObjectStateManager 中的另一个对象冲突。在调用 AcceptChanges 之前确保键值是唯一的。
这是我正在使用的保存方法。“SegmentID”列是“Segment”的 PK,它是一个整数,在 DB (MS SQL 2008) 中设置为自动递增。默认情况下,“SegmentID”设置为 0,直到它从 DB 中获取更新的段。
public bool Save(Treatment myTreatment)
{
bool result = false;
using (tamcEntities db = new tamcEntities())
{
// IF NEW TREATMENT, CREATE IT AND ADD TO DB
if (myTreatment.Treatment_ID == 0)
{
db.Treatments.AddObject(myTreatment);
result = (db.SaveChanges() != 0);
}
// IF EXISTING TREATMENT, FIND EXISTING TREATMENT IN DB, AND UPDATE IT
else
{
List<string> treatmentIncludes = new List<string>();
treatmentIncludes.Add("Segments");
Treatment myTmt = (from x in db.Treatments
where x.Treatment_ID == myTreatment.Treatment_ID
select x).WithIncludes(treatmentIncludes).FirstOrDefault();
if (myTmt != null)
{
myTmt.Comment = myTreatment.Comment;
myTmt.Cost = myTreatment.Cost;
myTmt.CostItemDrain = myTreatment.CostItemDrain;
myTmt.CostItemE2E = myTreatment.CostItemE2E;
myTmt.CostItemEnhan = myTreatment.CostItemEnhan;
myTmt.CostItemEnv = myTreatment.CostItemEnv;
myTmt.CostItemGuard = myTreatment.CostItemGuard;
myTmt.CostItemOther = myTreatment.CostItemOther;
myTmt.CostItemPed = myTreatment.CostItemPed;
myTmt.CostItemSub = myTreatment.CostItemSub;
myTmt.CostItemTraffic = myTreatment.CostItemTraffic;
myTmt.CostItemUtl = myTreatment.CostItemUtl;
myTmt.Create_DateTime = myTreatment.Create_DateTime;
myTmt.Create_Entity = myTreatment.Create_Entity;
myTmt.Create_User = myTreatment.Create_User;
myTmt.Description = myTreatment.Description;
myTmt.Improvement_Type = myTreatment.Improvement_Type;
myTmt.Jurisdiction = myTreatment.Jurisdiction;
myTmt.Last_Update_DateTime = myTreatment.Last_Update_DateTime;
myTmt.Last_Update_Entity = myTreatment.Last_Update_Entity;
myTmt.Last_Update_User = myTreatment.Last_Update_User;
myTmt.Life_Expectancy = myTreatment.Life_Expectancy;
myTmt.MDOTJobID = myTreatment.MDOTJobID;
myTmt.Planned = myTreatment.Planned;
myTmt.Project_Classification = myTreatment.Project_Classification;
myTmt.ProjectID = myTreatment.ProjectID;
myTmt.Quantity = myTreatment.Quantity;
myTmt.SurfaceTypeAfter = myTreatment.SurfaceTypeAfter;
myTmt.tmp_treat = myTreatment.tmp_treat;
myTmt.Treatment_Date = myTreatment.Treatment_Date;
myTmt.Unit_of_Measure = myTreatment.Unit_of_Measure;
// DELETE MISSING SEGMENTS THAT ARE NO LONGER PART OF THE TREATMENT
List<int> segmentIDsToKeep = myTreatment.Segments.Select(x => x.SegmentID).ToList();
myTmt.Segments.Where(x => !segmentIDsToKeep.Contains(x.SegmentID)).ToList().ForEach(x => db.Segments.DeleteObject(x));
// ITERATE OVER EACH SEGMENT AND INSERT OR UPDATE IT
foreach (Segment s in myTreatment.Segments)
{
if (!string.IsNullOrWhiteSpace(s.PR) && !string.IsNullOrWhiteSpace(s.BMP.ToString()) && !string.IsNullOrWhiteSpace(s.EMP.ToString()))
{
Segment mySegment = new Segment();
// IF EXISTING SEGMENT, FIND EXISTING SEGMENT IN DB, AND UPDATE IT
if (s.SegmentID != 0)
{
mySegment = (from x in myTmt.Segments
where x.SegmentID == s.SegmentID
select x).FirstOrDefault();
}
mySegment.ActualLength = s.ActualLength;
mySegment.BMP = s.BMP;
mySegment.Create_DateTime = s.Create_DateTime;
mySegment.Create_Entity = s.Create_Entity;
mySegment.Create_User = s.Create_User;
mySegment.EMP = s.EMP;
mySegment.HasRequiredHPMS = s.HasRequiredHPMS;
mySegment.Lanes = s.Lanes;
mySegment.Last_Update_DateTime = s.Last_Update_DateTime;
mySegment.Last_Update_Entity = s.Last_Update_Entity;
mySegment.Last_Update_User = s.Last_Update_User;
mySegment.PASER_Rating = s.PASER_Rating;
mySegment.PR = s.PR;
mySegment.RoadName = s.RoadName;
mySegment.SurfaceType = s.SurfaceType;
mySegment.Treatment_ID = s.Treatment_ID;
mySegment.Version = s.Version;
// If the BMP is greater than the EMP, swap them.
if (mySegment.BMP > mySegment.EMP)
{
decimal tempBMP = mySegment.BMP;
decimal tempEMP = mySegment.EMP;
mySegment.BMP = tempEMP;
mySegment.EMP = tempBMP;
}
// IF NEW SEGMENT, ADD IT
if (s.SegmentID == 0)
{
myTmt.Segments.Add(mySegment);
}
}
}
result = (db.SaveChanges(SaveOptions.AcceptAllChangesAfterSave) != 0);
}
}
}
return result;
}