迁移实体和父实体
我有一个实体 A,它有两个属性。实体 B 以 A 作为父级,并具有额外的 3 个属性。新版本中的更改不会影响实体 A 和 B。
如何将实体 B 的对象迁移到新版本的数据模型,包括实体 A 的属性?
我尝试使用两种实体映射:一种用于 A,一种用于 B,但“A 属性”未迁移。或者,我将 A 的属性添加到映射中以迁移 B,但在那里我无法选择正确的属性(在 Xcode 4 中)。
编辑:
我指的不是两个实体之间的常规关系,而是继承:
编辑2:
为了确定,我创建了一个新项目进行测试。在这里,我仅添加了如上所示的两个实体。在我的 awakeFromNib 中,我执行了一个获取请求,如果没有返回结果,我添加一个新实体:
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"];
在我的数据模型的第二个版本中,我添加了一个新实体,并通过
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)。
编辑3: 我刚刚尝试在 Xcode 3(.2.4) 中打开这个新制作的测试项目。当我打开其中的映射模型并选择子实体的映射时,我实际上可以为父实体的属性添加属性映射:
所以,我想这会使这成为 Xcode 4 中的一个错误。
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.
How can I migrate objects of entity B to a new version of my data model, including the attributes from entity A?
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).
Edit:
I'm not referring to a regular relationship between two entities, but inheritance:
Edit 2:
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]) {
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).
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:
So, I guess that would make this a bug in Xcode 4.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定出了什么问题。
我刚刚创建了这个数据模型版本 1,我希望这尽可能接近您的情况:
然后我创建了这个版本 2,对父级和子级没有任何更改,只有一个新实体:
我创建了一个映射模型,这就是它自动建议的内容:
让我们看看差异:
只有一处更改:新实体 Neighbour。
你能发一些照片来说明你的情况吗?
注意这是XCode3
I am not sure what went wrong.
I just created this data model version 1, I hope this gets as close to your case as possible:
Then I created this version 2, no changes to Parent and Child only one new entity:
The I created a mapping model and this is what it automatically suggested:
Let's have a look at the differences:
Only one change: the new entity Neighbour.
Can you post some pics of your situation?
Note This is XCode3
事实证明这是我当时使用的 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.