RIA Services: Enumerate Deleted Entities

北战南征 提交于 2019-12-11 14:03:40

问题


My RIA service context class has an entity set TaskToOperationAssociations which contains a list of Task to Operation associations.

Is there a way to "find" an association entity which has been removed from the collection? I can see that the context has a reference to the removed Association in it's private fields (it obviously needs to keep track of it so the delete operation can be submitted).

Here's an example...

If I have Task "A" (with Id=T1) which is associated to Operation X, Y and Z (with id's O1,O2 and O3) and task B (with Id=T2) is associated with the same operations then the collection will contain 3 TaskToOperationAssociations as follows...

  1. Association A1, TaskId = T1, OperationID = 1
  2. Association A2, TaskId = T1, OperationID = 2
  3. Association A3, TaskId = T1, OperationID = 3
  4. Association A4, TaskId = T2, OperationID = 1
  5. Association A5, TaskId = T2, OperationID = 2
  6. Association A6, TaskId = T2, OperationID = 3

I remove association A1 and catch the property change event of the TaskToOperationAssociations. In the event handler I want to find out if any of the associations for Task T1 have changed so I can enable a save button on the UI.

Hope this makes sense. Thanks Ben


回答1:


You can enumerate ChangeSets, ChangeSet will contain everything before it was submitted. On server side, you can intercept OnSaveChanges and also enumerate ChangeSet.

For existing association changes, mostly changeset will contain change of Foreign Key. Your current Context's Entity Container will contain ChangeSets that are not submitted yet.




回答2:


Maybe this can help you

var deleted = Context.EntityContainer.GetChanges().RemovedEntities
    .Where(re => re is TaskToOperationAssociations && ((TaskToOperationAssociations)re.GetOriginal()).TaskId == T1.Id)
    .Select(re => (TaskToOperationAssociations)re);

or just

var hasDeleted = Context.EntityContainer.GetChanges().RemovedEntities
    .Any(re => re is TaskToOperationAssociations && ((TaskToOperationAssociations)re.GetOriginal()).TaskId == T1.Id)

to find out if there are any deleted associations for T1

with

foreach (var assoc in deleted)
{
   Context.TaskToOperationAssociations.Add(assoc);
   ((IRevertibleChangeTracking)assoc).RejectChanges();
   ((IRevertibleChangeTracking)T1).RejectChanges();
}

you can completely undo the deletion (if you have not committed your changes yet)




回答3:


There is no way to do this using only the context. I fixed it by wrapping the context and tracking deletions manually.



来源:https://stackoverflow.com/questions/5155376/ria-services-enumerate-deleted-entities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!