This is a migrated thread and some comments may be shown as answers.

Deleting the entities and removing items from the collections

4 Answers 58 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Dilshod
Top achievements
Rank 1
Dilshod asked on 08 Jul 2016, 04:21 AM

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

Sort by
0
Kristian Nikolov
Telerik team
answered on 08 Jul 2016, 12:57 PM
Hello Dilshod,

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:
  1. Define a fetch strategy and assign it to the context object.
  2. 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
 
Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
0
Dilshod
Top achievements
Rank 1
answered on 29 Jul 2016, 06:54 AM

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

0
Kristian Nikolov
Telerik team
answered on 02 Aug 2016, 02:24 PM
Hello 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
 
Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
0
Dilshod
Top achievements
Rank 1
answered on 17 Aug 2016, 08:59 AM

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,

Tags
Development (API, general questions)
Asked by
Dilshod
Top achievements
Rank 1
Answers by
Kristian Nikolov
Telerik team
Dilshod
Top achievements
Rank 1
Share this question
or