RIAServices:在客户端编辑实体而不打算保存
我经常发现自己需要在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过调用轻松地从
DomainContext
中分离整个实体列表。在执行此操作之前,只需将实体复制到另一个列表中,以便稍后可以访问它们。 (我建议将它们保留在 ViewModel 中。)
简而言之,只需执行以下操作:
List
中。DomainContext.MyEntities.Clear();
SubmitChanges()
上发送回服务器,因为它们不再保存在DomainContext
中。You can easily detach the whole list of entities from the
DomainContext
by callingBefore 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:
List<MyEntity>
in your ViewModel.DomainContext.MyEntities.Clear();
SubmitChanges()
, because they are no longer kept in theDomainContext
.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.