tags:

views:

59

answers:

2

What is the best way to mark some entities DeleteOnSubmit(). Is there a way to check and say to the context that this is for deletion?

Example: I have an Entity which reference an EntitySet<> and i delete from the EntitySet<> 4 of the 8 entities. When submitting changes i want to say DeleteOnSubmit() on those 4! This scenario should play on a single EntityRef<> too.

Of course DataContext lives in another layer so...grabbing, changing, sending back is the job.

Thank you.

A: 

Take a look at DeleteAllOnSubmit(). You pass this method a list of entities to be deleted.

Randy Minder
I cant pass all my EntitySet for deletion, i will lose them all, maybe you mean of keeping track the entities Id in another List<> and use them to DeleteOnSubmit() those entities?
+1  A: 

This is pretty hard to answer based on the description of your architecture. Just because you're using a layered approach doesn't mean that you can't call DeleteOnSubmit... you'd just call your own method that wraps that I presume.

Unless, of course, you're instantiating your DataContext object in the update routine. in this case you'd have to do something else. Your data layer could expose a method like MarkForDelete() which just adds the entity to a collection, then expose a separate SubmitChanges() that iterates over the collected items for deletion, attaches them to the datacontext and then does the actual DeleteAllOnSubmit() call.

That said I've never really bothered with the whole entity serialization/deserialization/reattach thing as it seems fraught with peril. I usually just collect the primary keys in a list, select out the entities and re-delete them. It's no more work, really.

roufamatic
I used this approach yesterday...thanx for reply