使用 RIA 服务插入实体和依赖实体
我有一个关于使用 RIA 服务(使用 Silverlight 4.0)插入具有依赖实体的实体的问题。
假设我的 (sql) 数据库中有一个名为“Beer”的实体和一个名为“Supplier”的实体,其关系为:Beer 1 - n Supply。一种啤酒有多个供应商。
现在有以下用例:用户输入一种新啤酒,比方说,有 5 个供应商。
在 silverlight 视图上,我现在有两个 DomainDataSource
。在啤酒 DomainDataSource
上,我添加并提交新啤酒,在供应商 DomainDataSource
上,我提交当前的供应商,其中包含将它们链接到啤酒的外键。
我的问题是:我如何确保首先提交啤酒,然后提交依赖(记住外键)供应商?
我知道我可以简单地使用 OnSubscribed
事件链接 SubmitChanges()
。但这个解决方案相当……嗯……蹩脚。它会产生一些非常丑陋的代码。
感谢您提出的众多想法!
I got a question about inserting an entity with dependent entities using RIA Services (With Silverlight 4.0).
Let's say I have an Entity in my (sql) database called "Beer" and one called "Supplier", with a relationship: Beer 1 - n Supplier. There are multiple suppliers for one kind of beer.
Now there's the following use case: The user enters a new beer with, let's say, 5 suppliers.
On the silverlight view I now got two DomainDataSource
's. On the Beer DomainDataSource
I add and submit the new beer and on the Supplier DomainDataSource
I submit the now suppliers, which contain a foreign key which links them to the beer.
My question is: How can I make sure that the Beer gets submitted first and afterwards the dependent (remember the foreign key) Suppliers?
I am aware that I could simply chain up the SubmitChanges()
using the OnSubmitted
event. But this solution is pretty... well... lame. It makes for some really ugly code.
Thanks for all your numerous ideas!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,无法强制同一变更集中的更新顺序。
但是,如果所有新供应商都向服务器提交了新啤酒(一个很大的 IF),您可以在 Update 方法中手动检查 ChangeSet:
这将调用一个单独的方法来更新供应商。您仍然需要一个 UpdateSupplier 方法,否则当它存在于 ChangeSet 中时,RIA 将抛出异常,但该方法不应执行任何操作:
Unfortunately there is no way to force the order of the updates that come in the same ChangeSet.
However, if all new suppliers are submitted to the server with new beers (a big IF), you could manually check the ChangeSet in your Upddate method:
That calls a separate method to update supplier. You still need an UpdateSupplier method or RIA will throw an exception when it exists in the ChangeSet, but the method should do nothing: