Persisting collection deletions

Posts   
 
    
AlanD
User
Posts: 26
Joined: 19-Sep-2006
# Posted on: 17-May-2007 17:29:42   

Hi, I'm struggling to persist collection deletions to the database. I have a fairly complex object with various collection properties and I have a grid (Infragistics) pointing at one of the collections. If I add an item to the collection via the grid and Save() the object, the new item gets persisted to the database. If I delete an item via the grid, I can see it disappear from the collection, but that delete doesn't get persisted when I save, so when I retrieve the object from the database, the deleted item has come back.

I did a quick search on the forums and found the "AllowRemove" property. This is false on all my collections, so I set it to true just before the Save (but after the grid actually forced the deletion from the collection).

Unfortunately, it didn't work - no Sql was emitted. I created a new object in the collection, deleted a different one and saved; Sql insert code was emitted, but still no Sql delete code. Am I missing the point? Or is there something else I need to do before trying to save my object/collection?

Thanks,

Alan

jmeckley
User
Posts: 403
Joined: 05-Jul-2006
# Posted on: 18-May-2007 01:39:24   

Remove will only remove the item from the collection. you need to call the delete entity/collection function instead.

adapter: adapter.DeleteEntityCollection(collection); selfservice: Collection.Delete();

not 100% sure about self service, I only use adapter.

if you're using an LLBLDataSource, you can also just use the UoW.Commit(true); to commit all changes in 1 transaction.

AlanD
User
Posts: 26
Joined: 19-Sep-2006
# Posted on: 18-May-2007 09:26:08   

Thanks Jason,

Are you saying that after an item is added to the collection (using a grid), I only need to Save(), but after deleting an item (using a grid) I need to perform some sort of manual delete before the save to make sure it's persisted?

What seems to be happening is that - If I add an item to a collection via the grid, a Save() will produce a sql insert statement - If I delete an item from a collection via the grid, a Save() will NOT produce a sql delete statement

Incidentally, I am setting the IsDirty flag to true on the object just before Save.

Oops, I forgot to provide the background details: LLBLGen PRO designer: 1.0.2005.1.Final, July 6th 2006, SD.LLBLGen.Pro.ORMSupportClasses.NET20.dll version: 1.0.20051.60719 C# 2.0, VS2005 Self-servicing

Thanks,

Alan

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 18-May-2007 09:46:56   

You can put the removed entities in another collection, and delete it when saving. You can also use a Unit of Work to add entities for save and others for delete, and commit the UOW at the end.

note: LLBLGenDataSource is in v.2.0 and is used in ASP.NET apps.

AlanD
User
Posts: 26
Joined: 19-Sep-2006
# Posted on: 18-May-2007 10:04:32   

OK Walaa.

So, am I right in saying that there's no way Object.Save() will remove remove deleted items from the database and that I MUST do some sort of manual Delete for every underlying collection to make sure they go away?

There's nothing I can do at the object top level to cause all the contained collections to persist the deletion changes that have been made? Thanks, Alan

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 18-May-2007 10:35:49   

You are correct, there is nothing you can do here, except some manual work.

So I recommend using the UOW, where you will call UOW.Commit() at the end rather than Object.Save(). But still you would have to track deleted entities to put them in the UOW.

AlanD
User
Posts: 26
Joined: 19-Sep-2006
# Posted on: 18-May-2007 10:58:16   

Thanks Walaa - Will do.