更新时间:2023-12-01 17:50:40
此处的问题是因为两个或多个子存根具有相同的密钥:0。一旦尝试附加第一个对象,它将引发错误。
The problem here is because two or more child stubs have the same key: 0. Once you try to Attach the first object, it fires the error.
必须使用某种DTO重新设计该方法(我认为将ViewModel对象传递给Domain Model层是不正确的,这就是为什么我使用存根)。或直接从Controller调用添加/修改函数。
The method will have to be redesigned, using some kind of DTO (I think it's not correct to pass the ViewModel object to the Domain Model layer, thats why I was using stubs). Or calling a function to Add/Modify direct from the Controller.
编辑:
以下是代码:
public void EditReport(Inspection obj)
{
var inspection = new tbl_inspection
{
id_inspection = obj.ID,
code = obj.Code
};
foreach (var roll in obj.Rolls)
{
var rollStub = new tbl_inspection_roll
{
id_inspection_roll = roll.ID,
id_inspection = obj.ID,
description = roll.Description
};
container.tbl_inspection_roll.Attach(roll);
container.ObjectStateManager.ChangeObjectState(roll, (roll.id_inspection_roll == 0) ? EntityState.Added : EntityState.Modified);
}
container.tbl_inspection.Attach(inspection);
container.ObjectStateManager.ChangeObjectState(inspection, EntityState.Modified);
container.SaveChanges();
}
欢迎使用更好的解决方案...
Any better solutions are welcomed...