如何在实体框架中实现从实体到自身的多对多关联?

时间:2022-10-04 12:25:18

I have an entity that exposes a collection of children of the same type. The entity can itself be a child of another instance - in effect a many-to-many relationship with itself. In the database I have a linker table with two columns - parentId and childId - both of which reference the Id column in my entity table. Entity Framework correctly recognises that it is a many-tomany relationship with itself but throws an error:

我有一个实体,它暴露了同一类型的子集合。实体本身可以是另一个实例的子实体 - 实际上是与自身的多对多关系。在数据库中,我有一个包含两列的链接器表 - parentId和childId - 两者都引用了我的实体表中的Id列。实体框架正确地认识到它与自身有很多关系,但会引发错误:

Problem in Mapping Fragments starting at lines 249, 300: Two entities with possibly different keys are mapped to the same row. Ensure these two mapping fragments map both ends of the AssociationSet to the corresponding columns

从第249,300行开始映射片段中的问题:具有可能不同键的两个实体被映射到同一行。确保这两个映射片段将AssociationSet的两端映射到相应的列

Any ideas how I can accomplish this?

我有什么想法可以做到这一点?

1 个解决方案

#1


0  

I would set up a many-to-many relationship in a new EF model and generate a database from that. Then compare the structures of what it created to your database model. This should give some clues.

我会在新的EF模型中建立多对多关系并从中生成数据库。然后将它创建的结构与数据库模型进行比较。这应该给出一些线索。

#1


0  

I would set up a many-to-many relationship in a new EF model and generate a database from that. Then compare the structures of what it created to your database model. This should give some clues.

我会在新的EF模型中建立多对多关系并从中生成数据库。然后将它创建的结构与数据库模型进行比较。这应该给出一些线索。