CoreData从一对多迁移到多对多

发布于 2024-11-26 03:38:34 字数 1218 浏览 3 评论 0原文

我在将旧数据模型升级到当前模型时遇到问题。它有几个可能导致问题的层,我正在努力确定问题所在。

我有一个抽象的客户实体,其中包含与电话号码、电子邮件地址等的通用关系。在我的旧模型中,有一种关系,客户可以拥有 1 个房产(但一个房产可以有多个所有者),或者客户可以是租户在租约中(但租约可以有许多租户)。我现在已经更新了模型,以便客户可以拥有许多财产并成为许多租赁的一部分。

具体的客户端实体基本上将不同的命名信息添加到抽象中,因此有个人、商业、政府和导入(从其他系统导入)子类。

我的期望是,旧数据模型中建立的一对多关系将作为新数据模型中新的多对多关系的第一个实例添加。不幸的是,升级后的数据存储似乎不包含新的具体客户中的任何属性或租赁关系。

旧模型:

Client{
   Property<<-->Property.Owners
   Tenancy<<-->Lease.Tenants
}
ImportClient:Client{
   name:string
}

新模型:

Client{
  Properties<<-->>Property.Owners
  Tenancies<<-->>Lease.Tenants
}
ImportClient:Client{
  name:string
}

现在我可以看到可能出现的问题。首先,客户实体中的关系名称已从 Property 更改为 Properties,从 Lease 更改为 Leases。所以我添加了一个映射模型。该模型不会自动为 ClientToClient 添加实体映射(仅适用于具体类),因此我尝试自己添加一个。然而,我不确定如何设置值表达式,因此目前是:

 FUNCTION($manager,"destinationInstancesForEntityMappingName:sourceInstances:","PropertyToProperty","$source.Property")

如果我尝试将映射添加到具体类(因此 ImportClientToImportClient),则似乎绝对不可能正确设置关系值(被基本上是编辑器)。

所以我怀疑它要么无法传输关系,因为当针对客户端实体运行提取时它不会返回任何内容(每当我尝试过它时都是这种情况),或者我只是没有得到正确的值表达式。

我们将非常感谢您的帮助,因为目前这是阻止我发布该应用程序主要升级的唯一问题。

I have a problem in upgrading an old data model to a current model. It has a couple of layers which could be causing the problem and I'm struggling to determine where the issue lies.

I have an abstract Client entity which contains generic relationships to phone numbers, email addresses, etc. In my old model there was a relationship where a client could own 1 Property (but a Property could have many owners) or a client could be a tenant in a Lease (but a Lease can have many tenants). I've now updated the model such that a Client can own many Properties and be part of many Leases.

The concrete Client entities basically add different naming information to the abstract so there are Individual, Business, Government and Import (imported from other systems) subclasses.

My expectation was that the one-to-many relationship established in the old data model would be added as a first instance in a new many-to-many relationship in the new data model. Unfortunately the upgraded data store doesn't appear to contain any relationships in the new concrete clients for Properties or Leases.

The old model:

Client{
   Property<<-->Property.Owners
   Tenancy<<-->Lease.Tenants
}
ImportClient:Client{
   name:string
}

The new model:

Client{
  Properties<<-->>Property.Owners
  Tenancies<<-->>Lease.Tenants
}
ImportClient:Client{
  name:string
}

So now for the possible problems I can see. Firstly the relationship names have changed in the Client entity from Property to Properties and from Lease to Leases. So I've added a mapping model. The model didn't automatically add an Entity Mapping for ClientToClient (only for the concrete classes) so I've tried adding one myself. I'm not sure however how to set up the Value Expression so at the moment it's:

 FUNCTION($manager,"destinationInstancesForEntityMappingName:sourceInstances:","PropertyToProperty","$source.Property")

If I try and add the mapping to the concrete classes (so ImportClientToImportClient) it seems to be absolutely impossible to set the relationship values correctly (denied by the editor basically).

So my suspicions are that it's either failing to transfer the relationships because when the fetch is run against Client entity it returns nothing (whenever I've tried it this has been the case) or I'm just not getting the Value Expression right.

Help would be greatly appreciated because at the moment this is the only issue blocking the release of my major upgrade to the app.

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

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

发布评论

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

评论(1

自此以后,行同陌路 2024-12-03 03:38:34

因此,我找到的解决方案(对于那些后来偶然发现这个问题的人......)一般而言,因为我目前没有时间在这里详细说明所有代码。

步骤 1:查看是否可以在没有任何迁移选项的情况下打开数据存储。如果成功,请继续执行步骤 2,否则继续步骤 2。

步骤 2:使用 [NSPersistentStoreCoordinatormetadataForPersistantStore...] 检索存储元数据

步骤 3:从最新到最旧的顺序一次加载一个旧模型,然后使用 [NSManagedModel isConfiguration: compatableWithStoreMetadata:]直到找到有效的模型

步骤 4:使用有效的模型创建持久存储,然后从数据存储和持久存储创建托管对象上下文

步骤 5:缓存字典中失败的关系(我使用 UUID 对来标识对象)

步骤 6:使用当前的托管对象模型对数据存储执行轻量级升级

步骤 7:遍历字典,获取对象对并再次关联它们

是你必须使用自己的编码技能来实现这个(对于我的实例来说大约有 250 行代码),但希望这是你需要让它工作的种子......

So the solution as I've found it (for those who stumble across this later...) in general terms as I don't have time at present to detail all the code here.

Step 1: See if you can open your data store without any migration options. If that succeeds continue on, otherwise Step 2.

Step 2: Retrieve the store metadata with [NSPersistentStoreCoordinator metadataForPersistantStore...]

Step 3: Load your older models one at a time from most recent to oldest and use [NSManagedModel isConfiguration: compatibleWithStoreMetadata:] until you find a model that works

Step 4: Create a persistent store with the model that works and then create a managed object context from the data store and the persistent store

Step 5: Cache the failing relationship in a dictionary (I used pairs of UUIDs to identify the objects)

Step 6: Perform lightweight upgrade of the data store using your current managed object model

Step 7: Go through the dictionaries fetching the pairs of objects and associating them again

Yes you're going to have to use your own coding skills to implement this (it's about 250 lines of code for my instance) but hopefully this is the seed you need to get it working...

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