如何从 Silverlight RIA 服务执行服务器端插入/更新 (Upsert)

发布于 2024-09-24 18:40:17 字数 386 浏览 1 评论 0原文

虽然 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 技术交流群。

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

发布评论

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

评论(2

冷情妓 2024-10-01 18:40:17

好的,我有一个解决方案。我不确定这是否是正确的方法,因此将等待您的确认。

基本上,我重写默认的 RIA 服务插入方法并使其执行现有记录检查(与任何其他业务规则检查一样):

执行更新插入的客户端代码:

  • 创建域上下文
  • 创建一个可能已存在的新条目
  • 添加新项目到域上下文(到适当的表)
  • 提交更改(有效地请求插入 1 条记录)。

服务器端代码:

  • 替换现有的InsertTypeX(TypeX对象)方法。
  • 让 InsertTypeX 方法检查现有记录。
  • 如果存在现有记录,请更新该记录上的必填字段。
  • 如果记录不存在,则使用AddObject将该对象添加到EF表中。

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

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.

爱人如己 2024-10-01 18:40:17

我只是在存储过程中创建一个 MERGE 语句,然后映射函数导入以不返回任何内容。

I just create an MERGE statement in a stored procedure and then map the function import to return nothing.

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