如何与实体建立反向关系,它与它有两个以上的关系?

时间:2017-10-11 11:07:53

标签: entity-framework core-data

我有一个实体组和有关系的人:

Group:

Group.leader ->  Person (To One)
Group.looser ->  Person (To One)
Group.others ->> Person (To Many)

leaderlooserothers设置中,我可以拥有不同的Person个实体。同一个人可以在一个组中leader,在第二个组中looser,并在第三组中设置为others

Person实体中的

我有To-Many关系groups应该连接

Person:

Person.groups ->> Group (should be enough but warnings)

因为我总能做出一个反比关系  会有一个警告" 某些东西应该有反向"

如何处理这样的关系?

或者: 我有实体CubePlanLineCube有关系xyz,平面xy,仅限x。我需要在它们之间分享一些价值观,甚至有时是混合的:

Cube:
Cube.x --> Value
Cube.y --> Value
Cube.z --> Value

Plane:
Cube.x --> Value
Cube.y --> Value

Line:
Cube.x --> Value

Value:
Value.counted -->> Line.x or Line.y, Plane.x, Cube.x, y, z, SomeAnotherEntity.neededValue

1 个答案:

答案 0 :(得分:1)

Apple建议每个关系都应该有反向关系。在您的情况下,这意味着Person实体将有三种关系:

Person.groupsLed ->> Group (to many) // "groups where this Person is leader"
Person.groupsLost ->> Group (to many) // "groups where this person is the looser"
Person.otherGroups ->> Group (to many) // "other groups with this person as a member"

看起来确实相当复杂。另一种方法是将三个关系合并为一个(PersonGroup中的每一个)与一个中间实体(Ranking?):

Group.rankings ->> Ranking (to many) // "the ranking of people for this group"
Person.rankings ->> Ranking (to many) // "the ranking of this person in different groups"

在每种情况下,反向都是 - 1:

Ranking.person -> (Person) (to one) // "the person for this ranking"
Ranking.group -> (Group) (to one) // "the group for this ranking"

然后,您可以向Ranking实体添加属性以指示领导者/宽松者/其他人。这可能是一个简单的字符串属性rank,它取值“leader”,“looser”或“other”,或等效的整数枚举。要管理GroupPerson之间的关系,您可以添加或删除Ranking个对象。所有这一切的一个缺点是找到领导者或宽松者涉及过滤排名,但它确实给你一定程度的灵活性。