Deleting item from entity collection makes collection non-searchable

Posts   
 
    
RobertPhoenix avatar
Posts: 20
Joined: 14-Aug-2006
# Posted on: 18-Sep-2006 18:13:43   

I'm trying to delete one or more items from an entity collection.

The code I use is this



        private RelatedProductCollection collection;  // A collection class, generated by LLBLgen

        public void removeProduct(int productID)
        {
            IPredicateExpression selectFilter = new PredicateExpression(RelatedProductFields.ProductId == productID);
            List<int> matches = collection.FindMatches(selectFilter);
            foreach (int i in matches)
            {
                collection[i].Delete();
            }
        }

The problem that I find is that this will work once, but the second time I call the function I get an exception at the line

        List<int> matches = collection.FindMatches(selectFilter);

with the message

ORMEntityIsDeletedException

{"This entity is deleted from the database and can't be used in logic."}

I guess I'm not clear about what is deleted. Why wouldn't the FindMatches method work on a collection that is still in existence? (Even if some entries have been deleted)

What would be the correct way to implement such a delete?

Thanks for any help.

Robert.

Rogelio
User
Posts: 221
Joined: 29-Mar-2005
# Posted on: 18-Sep-2006 18:43:49   

Hi,

I see that you are deleting the entity from the DB; but you are not removing the entity from the collection. I think you have to remove the entity from the collection to avoid changing an entity that have been deleted.

RobertPhoenix avatar
Posts: 20
Joined: 14-Aug-2006
# Posted on: 18-Sep-2006 19:13:33   

So I added a 'remove' and that certainly makes it work! Thanks.

Then I removed the 'delete' and only left the 'remove' ( of the entity from the collection), followed by a 'save' of the collection at the end, but that doesn't reflect the delete through to the database.

Guess I'm going to have to work harder to understand this rage

Robert.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 18-Sep-2006 19:26:23   

You can also add the entities to a new collection and remove them from the actual collection, then simply delete all entities in the collection by calling the DeleteMulti() method.

Frans Bouma | Lead developer LLBLGen Pro