实体框架3.5使用()修改DbContext外部的对象创建

时间:2015-03-20 14:23:13

标签: winforms entity-framework .net-3.5

某处,我有

Order o = db.Orders.First(o => o.ID == 6);
...
o.Details.Add(new OrderDetail(1,2));

并按下按钮点击事件

...
Order o1 = db.Orders.FirstOrDefault(o2=>o2.ID == o.ID);
o1.Details.Clear();
foreach(OrderDetail od in o.Details){
o.Details.Add(od);
}
db.SaveChange();
...

我遇到了这个错误:

An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.Entity.dll

Additional information: The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects

似乎我无法修改由另一个DbContext或手工创建的对象,是否有工作方式?

0 个答案:

没有答案