2

I have many questions about how to make relationships N:M in CoreData.

If I have two entities A and B and have a relationship N:M in the entity relationship model has to generate a new table C that will contain the unique id of tables A and B.

Example for the entity relationship:

enter image description here

Now in the database model:

enter image description here

Considering the previous example as would be done by core data?

enter image description here

Or:

enter image description here

The truth is I'm really lost with that of relations in Core Data, any help will be welcome.

Sorry for my English but not very good.

4

1 回答 1

3

对此的答案是,如果您要存储附加信息,即使在 Core Data 中也必须使用“连接表”。您不必跟踪外键,因为这已包含在关系中。

通常,标准数据库“连接表”具有难看的名称,例如“tableA_tableB”。您应该努力找到一个合适的名称以使概念清晰。例如:

Car <<--->> Person
变成
Car <--->> Rental <<---> Person

现在在实体租赁中,您可以添加更多信息,例如时间。因此,例如,如果您想查找在某个日期之前租车的所有人(在 Rental 实体中存储为 NSDate),您的谓词将是这样的:

 [NSPredicate predicateWithFormat:@"ANY rentals.time < %@", cutoffTime]
于 2013-10-17T15:31:51.860 回答