Removing objects and their navigation properties

I have something like a Customer object with an order of up to 50,000 in an ICollection<Orders> . Suppose Custome is in the local cache, but orders are not. How can I delete Cutomer and all related orders without loading all customer orders into the cache and marking their setDeleted() ? What is the best practice here. I assume that extending the public SaveResult SaveChanges(JObject saveBundle) method is the best way. Any other features here on the client side, such as the delete_all_navigation_too() flag?

thanks

+6
source share
2 answers

The easiest approach I can come up with is to create a cascading delete restriction in the database so that when a client is deleted, all its orders are also deleted. Then simply delete the client on the client and call "SaveChanges". In addition, since Breeze does not yet support the removal of “cascaded” client side (we are considering this option), you will need to iterate over all orders on the client side that are already loaded and “disconnect” them.

+5
source

I must assume that you do not and do not need to cascade deletion in your database. Personally, I was "scared" of deletions in general and try to avoid them. I prefer soft deletion (marking the entry as inactive). But not everyone agrees or may follow suit.

I would like to add a Web API method to your controller (say, "DeleteCustomerAndOrders"). You can call any API method from your client, not just the Breeze method.

By recommending this, I suggest that such a thing is a relative rarity in your application. You do not need a general-purpose deactivator that accepts an array of identifiers of the parent objects that will delete some child objects and not others, ... etc. Etc.

Follow this path and you will transfer the problem from the client to the server. This is good: you did not need to load orders on the client. Now you need to get rid of them on the server. If you use the Entity Framework, you are faced with the same challenge of deleting orders without loading them. Check out Alex James's solution: Mass removal in LINQ for objects .

+6
source

All Articles