如何建立三元关系 [英] How to model ternary relationships
问题描述
例如,三个实体:
Bike
Crash
个人
然后会有一个三元关系知道:
- 哪个人在坠机X的自行车
- 参与自行车X和Y之间的事故X的人
- 在自行车B上遇到的人A发生什么事情
等。
我应该如何创建我的实体?
为三元关系创建新实体的唯一可能性?
选项1
假设有 Person-Bike
组合可以不存在 Crash
es(感谢上帝)。因此,假设自行车不是由特定人员拥有的,所以您可以创建具有FK对人和自行车的二进制关系(连接表) PersonBike
。 EF可以在模型中处理这种多对多的关系,而没有关联类。
然后,太糟糕了,人 - 自行车组合可能会涉及到崩溃。也许在一次崩溃中有两种以上的组合。基本上,您可以在崩溃
中收集 PersonBikes
。在这种情况下,模型中就没有关联类。但是,如果您想更多地了解 Crash
和 PersonBike
之间的关联,例如谁犯了崩溃,你需要一个连接表,两者之间: CrashPersonBike
与FK到崩溃
和 PersonBike
和描述关联的一个或多个字段(如有罪是/否)。这将需要EF模型中的关联类。
选项2
自行车具有特定的所有者(人):让人
有一个自行车
集合(表Bike有一个FK到人) 。现在只需描述所涉及的自行车的事故就足够了,因为自行车决定了人。所以崩溃
可以有一个自行车
集合(再次:模型中没有关联类)或关联类 CrashBike
,其中包含如上所述的更多详细信息。
选项3
在模型中需要关联类( CrashPersonBike
)的真实三元关系,有或没有额外的细节。
在所有情况下,您都需要业务逻辑来防止相同的人(或选项1和3中的自行车)参与一次崩溃。选项1和2还有一个额外的好处,因为该模型可以描述除了碰撞之外的人 - 自行车组合。是否应该是1或2取决于自行车人的所有权。
What's the best way to map entities to a ternary relationship with EF5?
For example, three entities:
Bike
Crash
Person
Then there would be a ternary relationship to know:
- The bike of which person was in the crash X
- The persons involved in the crash X between bikes X and Y
- In what crashes were involved person A on bike B
etc.
How should I create my entities? Is the only possibility to create a new entity for the ternary relationship?
Option 1
Suppose that there are Person-Bike
combinations that can exists without Crash
es (thank god). So you can create a binary relationship (junction table) PersonBike
having FK's to person and bike, assuming that bikes are not owned by specific persons. EF can handle this a a many-to-many relationship without an association class in the model.
Then, too bad, Person-Bike combinations can get involved in crashes. Maybe more than two combinations in one crash. Basically you could have a collection PersonBikes
in Crash
. In that case there would be no association class in the model at all. But if you want to know more about the association between Crash
and PersonBike
, e.g. who is guilty of the crash, you need a junction table between both: CrashPersonBike
with FK's to Crash
and PersonBike
and one or more fields describing the association (like guilty yes/no). This would require an association class in the EF model.
Option 2
Bikes have specific owners (persons): let Person
have a Bikes
collection (the table Bike has an FK to Person). Now it suffices to describe a crash only by the bikes that are involved, because the bikes determine the person. So Crash
can have a Bikes
collection (again: no association classes in the model) or an association class CrashBike
that contains more details as described above.
Option 3
A "true" ternary relationship that requires an association class (CrashPersonBike
) in the model, with or without extra details.
In all cases you need business logic to prevent the same persons (or bikes in option 1 and 3) from getting involved in one crash. Option 1 and 2 have an additional benefit in that the model can describe person-bike combinations apart from crashes. Whether it should be 1 or 2 depends on bike-person ownership.
这篇关于如何建立三元关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!