Hi,
I am trying to figure out whether if I can make DataAccess to remove the deleted item from the collection.
For example: I have a Student entity and Backpack that references Student entity. I am loading a Student entity that has bunch of Backpacks. Lets say student has three backpacks. I am trying to remove one of the backpacks. After the deleting is performed, the deleted item is still in the list. How can I make it so that it will disappear from the list?
I there way to accomplish that?
Thanks,
Dilshod
4 Answers, 1 is accepted
Thank you for contacting us.
It looks like you need to refresh the object which refers to the deleted one - this would be the student as per your example. In order to achieve this we would recommend trying the Refresh method of your context object (an instance of the OpenAccessContext class in your model) in a combination with a fetch strategy:
- Define a fetch strategy and assign it to the context object.
- Use the Refresh method to refresh the student.
Please keep in mind that the object you are refreshing must be managed by the context object you are using for the refresh.
I hope this helps. Feel free to get back to us in case you have further questions.Regards,
Kristian Nikolov
Telerik by Progress
Hi Kristian Nikolov,
I tried what you said, but it seems it won't refresh the list until the SaveChanges is called.
This is what I tried:
//Student has 5 backpacks.
var backpack = student.Backpacks.FirstOrDefault();
context.Delete(backpack);
//checking: student still has 5 backpaks
context.Refresh(RefreshMode.PreserveChanges, student);
//checking: student still has 5 backpacks
//Collection is only updating after the save is called.
Is there way to update the collection before the SaveChanges is called? Because we can't call save everytime we delete something and yet we need to get the collection items to calculate some things.
Thanks,
Dilshod
The behavior you are experiencing is normal. By calling the Delete method of the context (context.Delete(backpack)), you are just marking the entity for deletion. The delete operation itself is performed only after you call the SaveChanges method which commits the current transaction. This is the way Telerik Data Access is designed to handles transactions. Therefore, there is no way to have updated collection before performing the operation itself (via SaveChanges).
This would be the recommended order of operations:
//Student has 5 backpacks.
var backpack = student.Backpacks.FirstOrDefault();
context.Delete(backpack);
//Perform the delete
context.SaveChanges();
//checking: the student should have 4 backpacks now.
context.Refresh(RefreshMode.PreserveChanges, student);
I hope this helps. If you have any additional questions, you can get back to us via the forums.
Regards,
Kristian Nikolov
Telerik by Progress
Hello Kristian Nikolov,
I was able to achieve what I want by setting the association mapping configuration IsManaged(). That removes the item from the collection before I save my changes to the database.
Thanks,