Problem Deleting Records

Posts   
 
    
ercwebdev
User
Posts: 9
Joined: 17-Jul-2012
# Posted on: 26-Sep-2012 12:59:36   

Hello there, I'm having problems deleting some records. I've got a loop going and it goes through all the motions, getting the ID number and doing the delete. However when I look at the DB table, all the records are still there! Can anyone tell me what i'm missing in my code? Thanks, Alan R

   //Get List of Collections to be deleted 
            EntityCollection<ErcHeritageRelatedCollectionEntity> oldcollections = new EntityCollection<ErcHeritageRelatedCollectionEntity>();
            // fetch them using a DataAccessAdapter instance
            IRelationPredicateBucket filter = new RelationPredicateBucket();
            filter.PredicateExpression.Add(ErcHeritageRelatedCollectionFields.ResourceId == ID);
            filter.PredicateExpression.Add(ErcHeritageRelatedCollectionFields.DateAdded < timenow);
            using (DataAccessAdapter adapter = new DataAccessAdapter())
            {
                adapter.FetchEntityCollection(oldcollections, filter, 0, null);
                //Now delete the collections
                DataAccessAdapter adapter2 = new DataAccessAdapter(true);
                foreach (IEntity2 entity2 in oldcollections)
                {
                    int _delID = 0;
                    int.TryParse(entity2.Fields[0].CurrentValue.ToString(), out _delID);                
                    ErcHeritageRelatedCollectionEntity record = new ErcHeritageRelatedCollectionEntity(_delID);
                    adapter2.DeleteEntity(record);              
                }       
                  adapter2.CloseConnection();
            }
hotchill avatar
hotchill
User
Posts: 180
Joined: 22-Jan-2007
# Posted on: 26-Sep-2012 15:47:20   

Looks like you can use the following, no need to fetch entities to delete:


 IRelationPredicateBucket filter = new RelationPredicateBucket(ErcHeritageRelatedCollectionFields.ResourceId == ID & ErcHeritageRelatedCollectionFields.DateAdded < timenow);
using (DataAccessAdapter adapter = new DataAccessAdapter())  {
    adapter.DeleteEntitiesDirectly(EntityType.ErcHeritageRelatedCollectionEntity.ToString(), filter);
}

ercwebdev
User
Posts: 9
Joined: 17-Jul-2012
# Posted on: 26-Sep-2012 17:47:04   

That does the job nicely! Thank you, very much appreciated.