拒绝从 DomainContext 中删除的特定项目
我正在使用 Silverlight4 和 Ria 服务:
假设我们有一个包含 3 条记录( 1 , 2 , 3 )的表(称为“MyTable”),我刚刚在我的应用程序中的某处编写了以下代码:
CurrentItem = 1;
MyContext.MyTables.Delete(CurrentItem);
CurrentItem = 2;
MyContext.MyTables.Delete(CurrentItem);
出于某些原因,在点击之前“保存”按钮,我想拒绝第一个删除的项目(1),但仍然想删除第二个(2)。这意味着我不能使用:
MyContext.RejectChanges()
因为它将拒绝所有更改(包括已删除的项目)我确实想删除它),所以我使用 IRevertibleChangeTracking 可以解决我的问题。类似这样的事情:
((IRevertibleChangeTracking) MyItem).RejectChanges();
但是在使用此接口之前,我必须访问已删除的项目。起初,它尝试通过 MyContext.MyTables 获取它,但它不包含已删除的记录,所以我尝试通过 EntityChangeSet 获取它:
EntityChangeSet Changes = MyContext.EntityContainer.GetChanges();
MyTable DeletedItem = Changes.First<MyTables>( e => e.ID = 1 ) ;
然后我使用 IRevertibleChangeTracking:
((IRevertibleChangeTracking) DeletedItem ).RejectChanges();
但运行后,这行代码并没有改变状态该记录并被保留为“已删除”,因此通过点击“保存”按钮,它已从数据库中物理删除!!!! 看来 IRevertibleChangeTracking 不适用于已删除/添加的项目(它仅适用于修改的项目)。
那么,有什么方法可以拒绝 DomainContext 中的特定已删除项目。
谢谢,
I’m using Silverlight4 and Ria Service :
Imaging we have a table (called "MyTable") with 3 records ( 1 , 2 , 3 ) , I’ve just written the following codes somewhere in my application:
CurrentItem = 1;
MyContext.MyTables.Delete(CurrentItem);
CurrentItem = 2;
MyContext.MyTables.Delete(CurrentItem);
For some reasons, before hitting The “Save” Button, I want to reject the first deleted item(1) but still want to delete the second one(2) .it means that I can’t use :
MyContext.RejectChanges()
Because It will reject all changes (including the deleted item which what I do want to delete it) so I though, using IRevertibleChangeTracking can solve my issue .Something like this :
((IRevertibleChangeTracking) MyItem).RejectChanges();
But before using this Interface, I have to access the deleted Item. At first, It tried to get it via MyContext.MyTables but it doesn’t contain deleted records so I tried to obtain it by EntityChangeSet:
EntityChangeSet Changes = MyContext.EntityContainer.GetChanges();
MyTable DeletedItem = Changes.First<MyTables>( e => e.ID = 1 ) ;
And then I used IRevertibleChangeTracking:
((IRevertibleChangeTracking) DeletedItem ).RejectChanges();
But after Running, This line of code didn’t change the state of the record and it was kept as “Deleted” so by hitting the “Save” Button, It was deleted from the Database Physically !!!!
It seems IRevertibleChangeTracking doesn’t work for deleted/Added items ( it just works for modifed Items ).
So ,Is there any way to reject a particular deleted item from the DomainContext.
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过一番搜寻,我发现
您是否可以将布尔属性设置为 false,然后在完成后删除标记设置为
false
的实体,而不是删除每个实体?After a bit more hunting, I found where Colin Blair says:
Instead of deleting each entity, can you set a boolean property to false, and when finished, delete the entities with the flag set to
false
?