RIAServices:在客户端编辑实体而不打算保存

发布于 2024-11-05 14:53:59 字数 485 浏览 1 评论 0原文

我经常发现自己需要在 RIAServices 的客户端编辑实体,仅用于 UI 目的,而绝对无意将更改保存回数据库。但是,如果我没有在域服务中启用对该实体的编辑,那么当我尝试更改其中一个实体上的字段时,RIAServices 就会抛出错误,表示尚未在该实体上启用编辑。

但问题是我不想允许对数据库中的实体进行实际编辑,我只是​​想在客户端上搞乱它。我还没有找到一个像样的方法来解决我的问题,似乎唯一的选择是要么继续并启用实际编辑,并且永远不要调用 SubmitChanges() (这是一个非常可怕的想法),或者执行 foreach当我加载实体并创建每个实体的新实例并复制其所有属性时循环(基本上克隆我加载的实体,以便我不使用加载的 RIAServices),但这几乎不实际。

我正在寻找的(如果存在的话)是,在加载它们之后,以某种方式“分离”客户端上的实体列表,以便 RIAServices 停止监视它们,我可以随意编辑它们(我会丢失将它们救回来的能力,但这没关系,因为我无论如何都不需要或不想)。有什么想法吗?这让我抓狂。

I often find myself needing to edit an entity on the client side in RIAServices for mere UI purposes with absolutely no intention of saving the changes back to the database. However, if I don't enable editing of that entity in my domain service, RIAServices throws and error as soon as I try to change a field on one of my entities, saying that editing has not been enabled on that entity.

But the problem is I DON'T want to allow actual editing of the entity in the database, I simply want to mess around with it on the client. I have yet to find a decent way to solve my problem, and it seems like the only option is to either go ahead and enable actual editing, and just never call SubmitChanges() (which is a very scary idea), or do a foreach loop when I load in the entities and create a new instance of each entity and copy all of its properties over (basically clone my loaded entities so that I'm not using the ones RIAServices loaded), but this is hardly practical.

What I'm looking for (if it exists) is to, after I load them, somehow "detach" my list of entities on the client side, so that RIAServices stops monitoring them and I can edit them at will (I'll lose the ability to save them back, but that's ok since I don't need or want to anyway). Any ideas? This has been driving me nuts.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

泛滥成性 2024-11-12 14:53:59

您可以通过调用轻松地从 DomainContext 中分离整个实体列表。

DomainContext.MyEntities.Clear();

在执行此操作之前,只需将实体复制到另一个列表中,以便稍后可以访问它们。 (我建议将它们保留在 ViewModel 中。)

简而言之,只需执行以下操作:

  1. 通过 RIA 服务加载实体。
  2. 将加载的实体复制到 ViewModel 中的 List 中。
  3. 调用 DomainContext.MyEntities.Clear();
  4. 随意修改您的实体;它们不会在 SubmitChanges() 上发送回服务器,因为它们不再保存在 DomainContext 中。

You can easily detach the whole list of entities from the DomainContext by calling

DomainContext.MyEntities.Clear();

Before you do this, just copy the entities into another list so that you can access them later. (I suggest keeping them in the ViewModel.)

In short, just do this:

  1. Load the entities via RIA Services.
  2. Copy the loaded entities into a List<MyEntity> in your ViewModel.
  3. Call DomainContext.MyEntities.Clear();
  4. Modify your entities as much as you like; they will not be sent back to the server on SubmitChanges(), because they are no longer kept in the DomainContext.
十雾 2024-11-12 14:53:59

SubmitChanges 将简单地调用域服务上必要的插入、更新、删除方法,因此您所需要做的就是让这些方法调用抛出 new NotImplementedException(); 或完全删除这些方法。没有调用 SubmitChanges 的秘密方法。

SubmitChanges will simply call the necessary Insert, Update, Delete methods on your Domain Service so all you need to do is just make the methods call throw new NotImplementedException(); or just delete the methods altogether. There's no secret method for SubmitChanges that is being called.

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