如何在 XCode 中的 Core Data - 数据模型中表示外键关系

发布于 2024-12-22 06:11:40 字数 339 浏览 4 评论 0原文

我正在制作我的数据模型,这是核心数据的全新内容。我有 33 个实体,它们之间几乎没有硬关系,但有很多外键关系。

我如何管理核心数据模型中不完全是一对多、一对一或多对多而是外键的关系?

例如,我有一个联系人实体,它与 contact_x_mail 有关系,同时 contact_x_mail 与包含所有电子邮件的邮件有关系。这种关系是一对多或多对多。但还有其他的,比如Institution(一个联系人可以有多个Institution)和Mail,这不是一对多或1-1的关系,Institution有一个ForeignKey_mail_id。

我如何表示外键关系?索引?

非常感谢,希望我的问题很清楚。

Totally new with Core Data, I'm making my data model. I have 33 entities, and few hard relationships between them but lots of Foreign-Key relationships.

How can I manage that relationships that are not exactly 1-many or 1-1 or many-many but are foreign keys in Core Data Model?

For ex., I have a Contact entity, which has a relationship with a contact_x_mail, and at the same time contact_x_mail has a relationship with Mail, that contains all emails. This relationships are 1-many or many-many. But there are others like Institution (a contact can have many Institutions) and Mail, that is not a 1-many or 1-1 relationship, Institution has a ForeignKey_mail_id.

How can I represent that foreign key relationships? Indexes?

Thank you very much, hope my question is clear.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦晓ヶ微光ヅ倾城 2024-12-29 06:11:41

您将 CoreData 视为 DBMS,但事实并非如此。您不需要设置外键来在 CoreData 中建立关系。如果您想将电子邮件分配给用户,您只需在两者之间创建关系,并且可以设置用户的属性“电子邮件”或电子邮件的“用户”属性。 foreignKey和链接都是由CoreData在后台完成的。

另一方面,根据定义,每个关系都是 1-1、1-* 或 -。我不确定还有其他选择...

当您在 CoreData 中创建关系时,您实际上是在为该项目创建新属性。这是一个例子:

@interface User : NSManagedObject

#pragma mark - Attributes
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *emailAddress;

#pragma mark - Relationships
//All to-many relationships are saved as Sets. You can add to the "emails" relationship attribute to add email objects
@property (nonatomic, strong) NSSet     *emails;
//All to-one relationships are saved as types of NSManagedObject or the subclass; in this case "Institution"
@property (nonatomic, strong) Institution *institution;

设置这些就像这样简单:

User *user = [NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:[self.fetchedResultsController managedObjectContext]];
[user setName:@"Matt"];
[user setEmailAddress:@"[email protected]"];

//...Maybe i need to query my institution
NSFetchRequest *query = [[NSFetchRequest alloc] initWithEntityName:@"Institution"];
    [bcQuery setPredicate:[NSPredicate predicateWithFormat:@"id == %@",        institutionId]];
    NSArray *queryResults = [context executeFetchRequest:query error:&error];
[user setInstitution:[queryResults objectForId:0]];

//Now the user adds a email so i create it like the User one, I add the proper 
//attributes and to set it to the user i can actually set either end of the
//relationship
Email *email = ...
[email setUser:user];

//Here i set the user to the email so the email is now in the user's set of emails
//I could also go the other way and add the email to the set of user instead.

希望这有助于澄清一些事情!阅读文档以确保 CoreData 适合您!

http://developer.apple.com/library/mac /documentation/Cocoa/Conceptual/CoreData/CoreData.pdf

You are thinking of CoreData in terms of a DBMS which it is not. You don't need to set up foreign keys to make relationships in CoreData. If you want to assign an email to a user you just create a relationship of between the two and you can set the attribute "email" of a user or the "user" attribute of an email. The foreignKey and linking is all done by CoreData in the background.

On another point, every relationship is by definition, 1-1, 1-*, or -. I'm not sure there is any other choice...

When you create relationships in CoreData you are effectively creating new attributes for this item. Here is an example:

@interface User : NSManagedObject

#pragma mark - Attributes
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *emailAddress;

#pragma mark - Relationships
//All to-many relationships are saved as Sets. You can add to the "emails" relationship attribute to add email objects
@property (nonatomic, strong) NSSet     *emails;
//All to-one relationships are saved as types of NSManagedObject or the subclass; in this case "Institution"
@property (nonatomic, strong) Institution *institution;

Setting these is as simple as:

User *user = [NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:[self.fetchedResultsController managedObjectContext]];
[user setName:@"Matt"];
[user setEmailAddress:@"[email protected]"];

//...Maybe i need to query my institution
NSFetchRequest *query = [[NSFetchRequest alloc] initWithEntityName:@"Institution"];
    [bcQuery setPredicate:[NSPredicate predicateWithFormat:@"id == %@",        institutionId]];
    NSArray *queryResults = [context executeFetchRequest:query error:&error];
[user setInstitution:[queryResults objectForId:0]];

//Now the user adds a email so i create it like the User one, I add the proper 
//attributes and to set it to the user i can actually set either end of the
//relationship
Email *email = ...
[email setUser:user];

//Here i set the user to the email so the email is now in the user's set of emails
//I could also go the other way and add the email to the set of user instead.

Hope this helps clear things up a bit! Read up on the documentation to make sure CoreData is right for you!

http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/CoreData.pdf

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文