如何从Silverlight RIA服务器进行服务器端插入/更新(Upsert) [英] How to do a server-side Insert/Update (Upsert) from Silverlight RIA services

查看:170
本文介绍了如何从Silverlight RIA服务器进行服务器端插入/更新(Upsert)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然RIA服务似乎非常适合桌面操作和查询,我坚持一个传统的更新情况。 UPSERT (更新如果存在,否则插入新的):



首先:如果记录不存在,我想添加记录服务器端已经存在,否则如果已经存在,我想更新其当前的一个字段值。



第二:我不想从客户端查询数据库,查看记录是否存在。我只想在RIA服务上调用一个UpsertData方法,并且只能在服务器端进行添加或更新。



我尝试了很多选项,最接近我使用[Update(UsingCustomMethod = true)]方法,传递一个新创建的(因此分离的)实体。当调用我的对象的方法时,我得到:
不能在分离实体上调用自定义方法。



建议在最好的方式做这个将不胜感激:)

解决方案

好的,我有一个解决方案。我不知道这是否是正确的方法,所以将等待你自己的确认。



基本上我覆盖默认的RIA Services Insert方法,并使其执行现有的记录检查(像任何其他业务规则检查):



客户端代码执行Upsert:




  • 创建域上下文

  • 创建一个可能已经存在的新条目

  • 将新项目添加到域上下文(到适当的表格)

  • 提交更改(有效请求一个1记录插入)。



服务器端代码:




  • 替换现有的InsertTypeX(TypeX对象)方法。

  • 让InsertTypeX方法检查现有记录。

  • 记录存在,更新该记录上的必填字段。

  • 如果记录不存在,请使用AddObject将对象添加到EF表。



    • 现在看起来很简单,但也许有更好的方法来做到这一点。仍然提出意见和建议。


      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 :)

      解决方案

      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:

      • Create a domain context
      • Create a new entry, that might already exist
      • Add the new item to the domain context (to appropriate table)
      • Submit the changes (effectively request a 1 record insert).

      Server Side code:

      • Replace the existing InsertTypeX( TypeX object ) method.
      • Have the InsertTypeX method check for an existing record.
      • If an existing record exists, update the required fields on that record.
      • If a record does not exist, use AddObject to add the object to the EF table.

      Seems simple enough now, but maybe there is a better way to do this. Still open for comments and suggestions.

      这篇关于如何从Silverlight RIA服务器进行服务器端插入/更新(Upsert)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆