具有一对多或多对多关系的 RIA 服务表示模型
我正在尝试获取演示模型(已讨论 此处和此处)在 RIA 工作。我能找到的所有示例都是简单、平面的数据实体,没有一对多或多对多关系,这是我无法完成的工作 - 特别是更新和插入关联关系。
我可以正常工作的查询 - 我用关联属性(以及适当的情况下包含属性)标记了我的表示类,并且我对如何将数据加载到客户端并作为实体进行维护有很好的理解。我还插入了新实体。但是,我遇到了以下问题。对于以下示例,假设我们有简单的专辑和艺术家实体,其中专辑有单个艺术家,艺术家可以有零到多个专辑。两者都有一个 Name 属性。
- 在客户端,如果我执行 myArtist.Albums.Add(anAlbum) 或 myArtist.Albums.Remove(anAlbum),则不会发生任何情况。 HasChanges 返回 false。 (请注意,myArtist 和 anAlbum 仅在代码中通过加载实体并迭代以获取对特定实体的引用来获取:我还没有在 UI 或 DomainDataSources 中执行任何操作,只是闲逛)。
- 如果我更新艺术家和 SubmitChanges 上的名称,则当在服务器上调用 Update 方法时,专辑集合为空。
有谁有任何建议,或者您能给我指出一个使用更复杂对象的示例吗?
编辑(保留上述内容以供后代使用):好吧,看来第二个问题(在服务器上调用 Update 时对实体或实体集合的引用显示为 null)存在,因为子级实体没有被标记为已更改,因此它们不会被序列化并发回。我知道您可以通过使用 [组合] 来强制实现这种情况,并且我已经让它以这种方式工作,但这不是组合关系,我希望两个实体都是“顶级”实体。如何将实体标记为已更改?
I'm trying to get a presentation model (discussed here and here) working in RIA. All the examples I can find are simple, flat data entities with no 1-many or many-many relationships, which are what I can't get working - specifically, on updates and inserts into associative relationships.
Queries I can get working fine - I have my presentation classes marked up with Association attributes (and Include attributes, where appropriate), and I have a good understanding about how data is loaded into the client side and maintained there as entities. I also have inserts of new entities covered. However, I'm experiencing the following problems. For the following examples, assume we have simple Album and Artist entities, where an Album has a single artist and an Artist can have zero to many albums. Both have a Name property.
- On the client side, if I do myArtist.Albums.Add(anAlbum) or myArtist.Albums.Remove(anAlbum), nothing happens. HasChanges returns false. (Note that myArtist and anAlbum were obtained solely in code by loading the entities and iterating to get references to specific entities: I'm not doing anything in UI or with DomainDataSources yet, just dinking around).
- If I update the Name on an Artist and SubmitChanges, when the Update method gets called on the server, the Albums collection is null.
Does anyone have any suggestions, or can you point me to an example that uses more complex objects?
EDIT (keeping the above for posterity): Alright, it appears that the second issue (a reference to an entity or a collection of entities showing as null when Update gets called on the server) exists because the child entites aren't marked as Changed and so they aren't being serialized and sent back. I know you can force that to happen by using [Composition] and I have gotten it to work that way, but this is not a compositional relationship and I want both entities to be "top-level" entities. How can I mark an entity as changed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是我的 [关联] 属性未正确定义。我没有意识到关联两侧的关联名称属性必须相同。当名称相同并且您进行构建时,客户端上生成的代码将使用与“父级”使用的 EntityCollection 不同的构造函数来引用“子级”,这与关联设置不正确时的情况不同。新的构造函数采用回调,当您在集合上调用 Add 和 Remove 时,这些回调会执行一些额外的处理 - 具体来说,它们采用您要添加或删除的子实体,并修改其引用其父实体的属性,以便保留所有内容同步:您从中删除对象的集合、您将其添加到的集合以及对象对其父对象的引用。
The problem was that my [Association] attributes weren't correctly defined. I didn't realize that the association's Name property has to be the same on both sides of the association. When the names are the same and you do a build, the generated code on the client uses a different constructor for the EntityCollection used by the "parent" to refer to the "children" than it does if the associations aren't set up right. The new constructor takes callbacks that do a little bit of extra handling when you call Add and Remove on the collection - specifically, they take the child entity you are adding or removing and modify the property on it that refers to its parent so that everything remains in sync: the collection you removed the object from, the collection you added it to, and the object's reference to its parent.