迁移实体和父实体 [英] Migrating entities and parent entities
问题描述
我有一个具有两个属性的实体A.实体B具有A作为父对象,并具有其他3个属性.新版本中的更改不会影响实体A和实体B.
I have an entity A which has two attributes. Entity B has A as parent and has an additional 3 attributes. The changes in the new version don't affect entities A and B.
如何将实体B的对象迁移到数据模型的新版本,包括来自实体A的属性?
How can I migrate objects of entity B to a new version of my data model, including the attributes from entity A?
我尝试使用两个实体映射:一个用于A,一个用于B,但是'A属性'并未迁移.或者,我将A的属性添加到映射中以迁移B,但是在那里我无法选择正确的属性(在Xcode 4中).
I tried using two entity mappings: one for A and one for B, but 'A attributes' aren't migrated. Alternatively I would add A's attributes to the mapping to migrate B, but there I can't selected the right attributes (in Xcode 4).
我不是指两个实体之间的常规关系,而是继承:
I'm not referring to a regular relationship between two entities, but inheritance:
可以肯定的是,我创建了一个新项目进行测试.在这里,我仅添加了两个实体,如上所示.在我的 awakeFromNib
中,执行获取请求,如果未返回任何结果,则添加一个新实体:
Just to be sure, I created a new project to test with. Herein, I added only the two entities as seen above. In my awakeFromNib
I do a fetch request and if no results are returned, I add a new entity:
NSManagedObject *newAccount = [[NSManagedObject alloc] initWithEntity:entityDesc insertIntoManagedObjectContext:[self managedObjectContext]];
// Account
[newAccount setValue:@"TheName" forKey:@"name"];
[newAccount setValue:[NSDecimalNumber decimalNumberWithMantissa:5 exponent:2 isNegative:NO] forKey:@"currentBalance"];
// BankDebitAccount
[newAccount setValue:@"TheAccountNumber" forKey:@"accountNumber"];
[newAccount setValue:@"TheBankName" forKey:@"bankName"];
[newAccount setValue:[NSDecimalNumber decimalNumberWithMantissa:6 exponent:1 isNegative:YES] forKey:@"openingBalance"];
在第二个数据模型版本中,我添加了一个新实体,并通过启用了自动迁移
In my second version of the data model, I added a new entity and I enabled automatic migration via
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption];
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSXMLStoreType configuration:nil URL:url options:dict error:&error]) {
确实确实发生了迁移,并且成功迁移了BankDebitAccount的三个属性.Account中的currentBalance属性重置为0,并且name属性在XML文件中不再可见(因此等于nil).
The migration does indeed happen, and the three properties from BankDebitAccount successfully are migrated. The currentBalance property from Account is reset to 0 and the name property isn't visible in the XML file anymore (and thus, is equal to nil).
我只是尝试在Xcode 3(.2.4)中打开这个新创建的测试项目.当我在其中打开映射模型并选择子实体的映射时,实际上可以为父实体的属性添加一个属性映射:
Edit 3: I just tried opening this newly made test project in Xcode 3(.2.4). When I open the mapping model in there and select my child entity's mapping, I can actually add an attribute mapping for the parent's attributes:
所以,我想这会使它成为Xcode 4中的一个错误.
So, I guess that would make this a bug in Xcode 4.
推荐答案
结果证明这是我当时使用的Xcode版本中的一个错误,并已在Xcode 4.2中解决.
Turns out this was a bug in the version of Xcode I was using at the time and was resolved in Xcode 4.2.
这篇关于迁移实体和父实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!