这是一个相当简单的问题:当有人在我的ASP.NET MVC项目中点击“编辑计划”时 - 他不编辑,但创建一个新的计划。你可以在我的答案更清楚地看到它给我的qeustion这里:How do I duplicate an object?为什么我无法将对象添加到数据库? (mvc3)
现在我想要做同样的事情到它的引用,我做了这样的:
var featurePlans = db.FeaturePlanBaseSet.Where(f => f.PlanId == plan.Id).ToList();
db.PlanSet.AddObject(plan);
db.SaveChanges();
for (var i = 0; i < featurePlans.Count(); i++)
{
featurePlans[i].Plan = plan;
db.FeaturePlanBaseSet.AddObject(featurePlans[i]);
}
计划加入时,我做AddObject
,但Feature
不是。
我得到这个错误:
An object with the same key already exists in the ObjectStateManager. The existing object is in the Unchanged state.
我会很高兴知道它为什么会发生。