核心数据关系可以具有属性吗 [英] Can a Core Data Relationship Have Attributes

查看:142
本文介绍了核心数据关系可以具有属性吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将MySQL数据库移植到Mac OS应用程序的Core Data中.我的数据库中有两个多对多表.除了包含外键之外,还有一些数据列.是否可以向核心数据中的多对多关系添加属性?在我看来,它看起来并不像.我的后备方法是在Core Data中复制链接表.这样做有什么问题吗?

I'm porting a MySQL database to Core Data for a Mac OS app. I have two many to many tables in my database. In addition to containing the foreign keys, there are a few data columns. Is it possible to add attributes to a many to many relationship in Core Data? It doesn't look like it to me. My fallback is to replicate the linkage table in Core Data. Are there any problems doing this?

一个例子: 唱片中有一位或多位艺术家在表演. 艺术家执行零个或多个记录. 链接表行包含用于记录的外键,用于艺术家的外键,演奏者演奏的乐器以及用于添加其他信息的注释栏,该附加信息可跟踪演奏者的演奏.

An example: A record has one or more artists performing on it. An artist performs on zero or more records. The linkage table row contains a foreign key for the record, a foreign key for the artist, the instruments the player performed with, and a notes column that adds additional information such has which track the artist performed on.

推荐答案

您是正确的:关系本身不能具有属性.而且您在将链接表建模为中间实体时处于正确的轨道. CoreData编程指南关于基于关系的语义建模关系"的部分.在这种情况下,他们使用具有ranking属性的中间FriendsInfo实体对从PersonPerson的(自反)多对多关系建模.

You are correct: relationships themselves cannot have attributes. And you are on the right track in modelling the linking table as an intermediate entity. This approach is alluded to in the CoreData Programming Guide section on "Modelling a relationship based on its semantics". In their case, they model a (reflexive) many-many relationship from Person to Person using an intermediate FriendsInfo entity with a ranking attribute.

在您的示例中,您可能有一个Record实体,一个Artist实体和一个中间Appearance实体. Appearance实体将具有乐器和音符的属性,并具有与RecordArtist的(一对一)关系(每个具有一对多的逆关系).

In your example, you might have a Record entity, an Artist entity, and an intermediate Appearance entity. The Appearance entity would have attributes for Instruments and Notes, and (to-one) relationships to Record and Artist (each with a to-many inverse).

轻微的缺点是必须创建Appearance对象才能链接Record对象和Artist对象,而不仅仅是将它们添加到相关关系中.如果这对您很重要,您还必须注意Record/Artist组合的唯一性:默认情况下,同一RecordArtist可能会有很多Appearances.

The slight downside is that you have to create the Appearance object in order to link a Record object and an Artist object, rather than just adding them to the relevant relationship. You will also have to watch for uniqueness of the combination of Record/Artist, if that's important to you: by default there could be many Appearances for the same Record and Artist.

这篇关于核心数据关系可以具有属性吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆