Delete an item from the database when removing it from a collection

Posts   
 
    
changomarcelo avatar
Posts: 62
Joined: 15-Feb-2007
# Posted on: 12-May-2009 22:40:47   

I have the following scenario:

VendorEntity vendor = new VendorEntity(1); vendor.Products.Remove(product); vendor.Save(true);

When I use the Remove method to remove an item from a collection it does not get deleted from the database when I call Save(true).

How should I do it?

(I´m using version 2.6).

rdhatch
User
Posts: 198
Joined: 03-Nov-2007
# Posted on: 12-May-2009 22:50:54   

Hi Chango -

Easy. There are essentially 2 ways to do a delete: In-Memory, or Immediately.

If you want to do an immediate delete. Do myEntity.Delete().

If you want to delete in-memory and commit it later - you will want to use the myCollection.RemovedEntitiesTracker. So, whenever you do myCollection.Remove(myEntity)... myEntity will be automatically removed from myCollection and will be added into myCollection.RemovedEntitiesTracker.

Then, when you're saving - simply do: myCollection.RemovedEntitiesTracker.DeleteMulti().

Hope this helps!

Ryan