实体框架 4.0 和自引用表; .SaveChanges() 在 .AddObject() 上失败
问题摘要:
当使用自引用关系并尝试添加对象时,.SaveChanges() 失败并显示“无法确定 'PlannerModel.FK_PlanItem_PlanItem' 关系的原则结束。多个添加的实体可能具有相同的主键。”。
问题详细信息:
plannerContext = new PlannerEntities2();
var unitPlanQuery = from d in plannerContext.UnitPlans
where d.TeacherId == sourceTeacherId
orderby d.TeacherId
select d;
var planItem = new PlanItem();
ClonePlanItem(pi, planItem); // where pi is original PlanItem
planItem.ParentPlanItem = (PlanItem)planItemsAddedHT[pi.ParentPlanItemId];
// above object on right is the previously added PlanItem
plannerContext.PlanItems.AddObject(planItem);
plannerContext.SaveChanges();
我回到我的代码并对其进行了注释,以便我确定只发生了对“plannerContect.PlanItems.AddObject(planItem)”的一次调用。因此只需插入一个对象。错误消息更改为:
“无法确定相关操作的有效顺序。由于外键约束、模型要求或存储生成的值,可能会存在依赖关系。”
我返回并在 SQL Management Studio (SQL Server 2008 btw) 中的 ParentPlanItemId 列中添加了“Allow Nulls”,并刷新了我的模型……但这并没有什么区别。
表:PlanItem
PlanItem int PK,身份
ParentPlanItemID int ,允许 null
ItemText varchar(200)
来自模型设计器的引用约束:Principal = PlanItem;主键 = PlanItemId;从属属性 = ParentPlanItemId
来自模型设计器的关联:
AssociationSet 名称:FK_PlanItem_PlanItem
End1 多重性:1(PlanItem 之一)
End1 导航道具:PlanItem1
End2 多重性:*(PlanItem 的集合)
End2 导航:ParentPlanItem
名称:FK_PlanItem_PlanItem
PROBLEM SUMMARY:
When using self-referencing relationships and attempting to add objects, .SaveChanges() fails with “Unable to determine the principle end of the ‘PlannerModel.FK_PlanItem_PlanItem’ relationship. Multiple added entities may have the same primary key.”.
PROBLEM DETAILS:
plannerContext = new PlannerEntities2();
var unitPlanQuery = from d in plannerContext.UnitPlans
where d.TeacherId == sourceTeacherId
orderby d.TeacherId
select d;
var planItem = new PlanItem();
ClonePlanItem(pi, planItem); // where pi is original PlanItem
planItem.ParentPlanItem = (PlanItem)planItemsAddedHT[pi.ParentPlanItemId];
// above object on right is the previously added PlanItem
plannerContext.PlanItems.AddObject(planItem);
plannerContext.SaveChanges();
I went back to my code and commented it up such that I knew for sure only a single call to ‘plannerContect.PlanItems.AddObject(planItem)’ was taking place. Thus there was only a single object to insert. The error message changed to:
“Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values.”
I went back and added “Allow Nulls” to the ParentPlanItemId column in SQL Management Studio (SQL Server 2008 btw), and refreshed my model…but this did not make a difference.
Table: PlanItem
PlanItem int PK, identity
ParentPlanItemID int , allow null
ItemText varchar(200)
Referential Constraint from Model Designer: Principal = PlanItem; Principal Key = PlanItemId; Dependant Property = ParentPlanItemId
Association from Model Designer:
AssociationSet Name: FK_PlanItem_PlanItem
End1 Multipllicity: 1(One Of PlanItem)
End1 Navigation prop: PlanItem1
End2 Multuplicity: * (Collection of PlanItem)
End2 Nav: ParentPlanItem
Name: FK_PlanItem_PlanItem
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过直接设置
属性?
实际上,您可以将其设置为数据库中已存在的特定计划项目标识符(据我所知,您之前已经拥有了该标识符)。我并不是说这会起作用,但值得一试。我已经多次使用这些
*Reference
属性来加快插入速度,有时还加快读取速度。检查这篇博文以供进一步参考。
Have you tried setting directly
property?
You would actually set it to a particular plan item identifier (that you already have from before as I understood you) that already exists in the DB. I'm not saying it would work, but it's worth a try. I've used these
*Reference
properties several times to speed up the process if insertion and sometimes also reading.Check this blog post for further reference.
事实证明,像我一样设置父属性并不是问题。对于这样的自引用表,重数必须为(零或 1),并且 ParentPlanItemId 的属性需要允许空值。
祝大家干杯!
Turns out, setting the parent property like I was doing was not the problem. For a self-referencing table like this, the multiplicity needed to be (zero or 1) and the property of ParentPlanItemId needed to allow nulls.
Cheers to all!