问题描述
例如:如果我有两个实体Person和Car,并且每个都应该与一个Image实体相关联 - 这将是什么样子?
Example: If I had two entities Person and Car, and each of them should be linked with an Image entity - how would this have to look like?
现在,我只做三个实体:Person,Car,Image。所以人和车可以链接到一个图像实体。他们与图像实体有1:1的关系。
Right now, I would make just three Entities: Person, Car, Image. So persons and cars can link to an Image entity. They have a 1:1 relationship to the Image entity.
但是现在,一个核心数据伙计说,关系必须两种方式建模。这是蠢还是不?我需要两个图像实体。 CarImage和PersonImage。或者一个奇怪的Image实体,它有两个关系:一个到一个Car实体,一个到一个Person实体。
But now, a Core Data dude said that relationships have to be modeled both ways. That's stupid or not? I need two Image entities then. CarImage and PersonImage. Or one weird Image entity which has two relationships: One to a Car entity, and one to a Person entity.
如何建模?
推荐答案
简要版本
以两种方式建模。这不意味着你需要两个图像实体。如果一个人与图像存在单向关系,并且您删除了图像(它没有反向关系,因此不知道该人),您的图形现在已损坏。
Yes the relationships need to be modelled both ways. No that doesn't mean you need two Image entities. If a person had a one-way relationship to an image, and you deleted the image (which doesn't have an inverse relationship, so knows nothing of the person) your graph is now corrupt.
所以你需要双向关系,但这并不意味着你需要两个图像实体。您的图片只需要一个关系 - 容器,例如,可以是个人或汽车(共享父实体会帮助)。
So you need two way relationships, but that doesn't mean you need two image entities. Your Image just needs one relationship - 'container', say, that could be a Person or a Car (a shared Parent entity will help).
版本..
这篇关于我真的需要始终在Core Data中的双向关系吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!