.Delete() doesn't work for newly created Entities

Posts   
 
    
Waveslam
User
Posts: 18
Joined: 20-Nov-2006
# Posted on: 29-Nov-2006 23:09:00   

Hi I have (basically) a Master-Detail type windows form, mirroring a 1:n relationship between 2 tables in my DB; Bug and BugDetail. The UI is such that the user can select a Bug record, and then edit both it and related BugDetail records. The whole lot is then saved using a recursive .Save() on the Bug Entity.

I am using databinding so that the edit fields for the detail portion of the UI are bound to BugDetail entity objects. When the user wants to create a new BugDetail row, I must create a new BugDetail entity object in order that I can bind the UI controls to the relevant properties of the entity.

My problem is that in the event the user doesn't actually enter anything for the newly-created BugDetail entity, I dont want to insert it into the DB, as its a blank record.

So, just prior to the

.Save(true); 

call on the Bug entity, I am iterating through the BugDetail collection, calling

.Delete() 

on anything that is blank. (This also removes any existing records where the user has removed all detail). However, it doesn't seem to work for newly-created entities: the blank BugDetail record gets saved anyway.

I have also tried using

.CancelEdit();

, which is not mentioned in the LLBLGen doco but had a very helpful-sounding Intellisense description! Sadly, that doesn't work either.

So, what am I doing wrong? I am using SelfServicing for .NET 2.0, LLBLGenPro version 2.0.0 DEMO, C#, SQL Server 2000, Windows Forms.

Cheers Brett

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 30-Nov-2006 02:29:20   

You can continue to call the delete for entities that existed before if you want to continue to remove them from the DB. For the new entities they will also have the IsNew value set to true. If it is blank and IsNew is true then remove the entity from the collection. Then when you call save it will not be sent to the DB.

Waveslam
User
Posts: 18
Joined: 20-Nov-2006
# Posted on: 30-Nov-2006 02:48:42   

Hi Thanks for the reply. I tried to do what you suggested but not having much luck. First I tried this:


            RichTextBox rtb = new RichTextBox();
            foreach (IbBugDetailEntity ibBugDetailRow in ibBugRow.IbBugDetail)
            {
                rtb.Rtf = ibBugDetailRow.BdInfo;
                if (rtb.Text == "")
                {
                    if (ibBugDetailRow.IsNew)
                    {
                        ibBugRow.IbBugDetail.Remove(ibBugDetailRow);
                    }
                    else
                    {
                        ibBugDetailRow.Delete();
                    }
                }
            }
            ibBugRow.Save(true);

But this failed when deleting one of the newly created rows with:

InvalidOperationException was unhandled
Collection was modified; enumeration operation may not execute.

I understand this is because I am trying to modifying the collection while I am iterating through it. SO, then I tried copying all the items to delete to a separate list and deleting them one by one with:


            RichTextBox rtb = new RichTextBox();
            IbBugDetailCollection BlankibBugDetailList = new IbBugDetailCollection(); ;
            foreach (IbBugDetailEntity ibBugDetailRow in ibBugRow.IbBugDetail)
            {
                rtb.Rtf = ibBugDetailRow.BdInfo;
                if (rtb.Text == "")
                {
                    BlankibBugDetailList.Add(ibBugDetailRow);
                }
            }

            foreach (IbBugDetailEntity ibBugDetailRow in BlankibBugDetailList)
            {
                if (ibBugDetailRow.IsNew)
                {
                    ibBugRow.IbBugDetail.Remove(ibBugDetailRow);
                }
                else
                {
                    ibBugDetailRow.Delete();
                }

            }
            ibBugRow.Save(true);


This worked up until I executed the .Save() call, at which point I got:

ORMCOncurrencyException was unhandled
During a save action an entity's update action failed. The entity which failed is enclosed

So, what is the recomended method of removing entities from an Entity Collection??

Thanks again Cheers Brett

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 30-Nov-2006 07:58:47   

This worked up until I executed the .Save() call, at which point I got: Code: ORMCOncurrencyException was unhandled During a save action an entity's update action failed. The entity which failed is enclosed

So, what is the recomended method of removing entities from an Entity Collection??

This error may have nothing to do with the removed entities. I think it has to do with an existing entity that is getting updated in the database. New entities should be Inserted not updated if they still exist in the collection, but I believe they have been correctly removed.

Waveslam
User
Posts: 18
Joined: 20-Nov-2006
# Posted on: 30-Nov-2006 21:10:42   

OK thanks. I actually got it working in the end by enclosing the .Delete() and .Save() calls in a single UoW object. However, I have a nasty suspicion that actually now works because of some other change I made (i tinkered with a few parts of the program, including the code that populates new Bug Detail rows...) but hey, it works now and thats the main thing, right..? ;-)

Thanks for the help though. Cheers B