如何从 Silverlight RIA 服务执行服务器端插入/更新 (Upsert)
虽然 RIA 服务对于表操作和桌面操作来说似乎非常好。查询时,我陷入了一种传统的更新情况。 UPSERT(如果存在则更新,否则插入新):
第一:如果记录尚不存在,我想在服务器端添加一条记录,否则如果它已经存在,我想更新其中一个其当前字段值。
第二:我不想从客户端查询数据库来查看记录是否存在。我只想在 RIA 服务上调用“UpsertData”方法,并且仅在服务器端进行添加或更新。
我尝试了很多选项,最接近的是我使用的 [Update(UsingCustomMethod = true)] 方法,传递一个新创建的(因此分离的)实体。当使用我的对象调用该方法时,我得到: “无法在分离实体上调用自定义方法。”
如果能提供有关最佳方法的建议,我们将不胜感激:)
While RIA services seems very good for table operations & queries, I am stuck on one traditional update situation. The UPSERT (Update if exists, else Insert new):
First: I want to add a record server-side if the record does not already exist, otherwise if it already exists, I want to update one of its current field values.
Second: I do not want to query the database from client-side, to see if the record exists. I just want to call an "UpsertData" method on RIA services and have the add or update occur server-side only.
I have tried many options, the closest I got used an [Update(UsingCustomMethod = true)] method, passing a newly created (therefore detached) Entity. When call the method with my object I get:
"A custom method cannot be invoked on a Detached Entity."
Suggestions on the best way to do this would be appreciated :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,我有一个解决方案。我不确定这是否是正确的方法,因此将等待您的确认。
基本上,我重写默认的 RIA 服务插入方法并使其执行现有记录检查(与任何其他业务规则检查一样):
执行更新插入的客户端代码:
服务器端代码:
现在看起来很简单,但也许有更好的方法来做到这一点。仍然开放征求意见和建议。
OK, I have a solution. I'm not sure if this is the correct way to do it, so will await confirmation from your good selves.
Basically I override the default RIA Services Insert method and make it do the existing record check (like any other business rule check):
Client Side code to do Upsert:
Server Side code:
Seems simple enough now, but maybe there is a better way to do this. Still open for comments and suggestions.
我只是在存储过程中创建一个 MERGE 语句,然后映射函数导入以不返回任何内容。
I just create an MERGE statement in a stored procedure and then map the function import to return nothing.