实体框架 4.0 和自引用表; .SaveChanges() 在 .AddObject() 上失败

发布于 2024-11-01 15:50:24 字数 1412 浏览 0 评论 0原文

问题摘要:

当使用自引用关系并尝试添加对象时,.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无尽的现实 2024-11-08 15:50:24

您是否尝试过直接设置

planItem.ParentPlanItemReference

属性?

实际上,您可以将其设置为数据库中已存在的特定计划项目标识符(据我所知,您之前已经拥有了该标识符)。我并不是说这会起作用,但值得一试。我已经多次使用这些 *Reference 属性来加快插入速度,有时还加快读取速度。

检查这篇博文以供进一步参考。

Have you tried setting directly

planItem.ParentPlanItemReference

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.

爱给你人给你 2024-11-08 15:50:24

事实证明,像我一样设置父属性并不是问题。对于这样的自引用表,重数必须为(零或 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文