迁移实体和父实体

发布于 2024-11-09 17:20:21 字数 1815 浏览 0 评论 0原文

我有一个实体 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) 中打开这个新制作的测试项目。当我打开其中的映射模型并选择子实体的映射时,我实际上可以为父实体的属性添加属性映射:

Child's Xcode 3 中的实体映射

所以,我想这会使这成为 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:
Image showing client and parent entity


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:

Child's entity mapping in Xcode 3

So, I guess that would make this a bug in Xcode 4.

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

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

发布评论

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

评论(2

顾铮苏瑾 2024-11-16 17:20:21

我不确定出了什么问题。

我刚刚创建了这个数据模型版本 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:

enter image description here

Then I created this version 2, no changes to Parent and Child only one new entity:

enter image description here

The I created a mapping model and this is what it automatically suggested:

enter image description here

Let's have a look at the differences:

enter image description here

Only one change: the new entity Neighbour.

Can you post some pics of your situation?

Note This is XCode3

只为一人 2024-11-16 17:20:21

事实证明这是我当时使用的 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.

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