在代码中创建 ManageObjectModel 时,我正在创建两个实体及其属性。但问题是我如何在两个实体之间创建关系 1 到多个关系。我的代码如下。我只想使用代码在两个员工和组织实体之间建立一对多的关系。
- (NSManagedObjectModel *)managedObjectModel
{
if (__managedObjectModel != nil) {
return __managedObjectModel;
}
__managedObjectModel = [[NSManagedObjectModel alloc] init];
NSEntityDescription *employeeEntity = [[NSEntityDescription alloc] init];
[employeeEntity setName:@"Employee"];
[employeeEntity setManagedObjectClassName:@"Employee"];
NSEntityDescription *organizationEntity = [[NSEntityDescription alloc] init];
[organizationEntity setName:@"Organization"];
[organizationEntity setManagedObjectClassName:@"Organization"];
[__managedObjectModel setEntities:[NSArray arrayWithObjects:employeeEntity, organizationEntity, nil]];
NSAttributeDescription *nameAttribute = [[NSAttributeDescription alloc] init];
[nameAttribute setName:@"name"];
[nameAttribute setAttributeType:NSDateAttributeType];
[nameAttribute setOptional:NO];
NSAttributeDescription *idAttribute = [[NSAttributeDescription alloc] init];
[idAttribute setName:@"id"];
[idAttribute setAttributeType:NSInteger32AttributeType];
[idAttribute setOptional:NO];
NSArray *properties = [NSArray arrayWithObjects: nameAttribute, idAttribute, nil];
[employeeEntity setProperties:properties];
NSAttributeDescription *organizationNameAttribute = [[NSAttributeDescription alloc] init];
[organizationNameAttribute setName:@"Name"];
[organizationNameAttribute setAttributeType:NSStringAttributeType];
[organizationNameAttribute setOptional:NO];
properties = [NSArray arrayWithObjects:organizationNameAttribute, nil];
[organizationEntity setProperties:properties];
return __managedObjectModel;
}