new entity added to a childcollection in OnBeforeEntitySave is not inserted

Posts   
 
    
arjanv
User
Posts: 506
Joined: 15-Nov-2006
# Posted on: 15-Dec-2006 09:58:49   

LLBLGen 2.0 build: October the 23rd - Adapter

Hi,

In the override of the OnBeforeEntitySave I add an entity to a collection of the saved entity. The newly added entity doesn't get saved. Is this suppose to work like this?

protected override void OnBeforeEntitySave()
{
       base.OnBeforeEntitySave();
       if (this.IsNew)
       {
              BtwHistorieRegelEntity nieuwBtwHistorieRegel =
                    new BtwHistorieRegelEntity();
              nieuwBtwHistorieRegel.IngangsDatum = new DateTime(1900, 1, 1);
              nieuwBtwHistorieRegel.EindDatum = new DateTime(2099, 1, 1);
              nieuwBtwHistorieRegel.Percentage = this.Percentage;
              nieuwBtwHistorieRegel.DebiteurenRekeningNr = this.DebiteurenRekeningNr;
              nieuwBtwHistorieRegel.CrediteurenRekeningNr = this.CrediteurenRekeningNr;
              this.HistorieRegels.Add(nieuwBtwHistorieRegel);
       }
}

Thanks, Arjan Vermunt

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 15-Dec-2006 11:24:39   

That method is meant for last minute changes to the entity itself, e.g. field defaults etc. Not for setting related entities, because it's called from the queue persister, so the queues are already calculated.

If you want to do that, do the following. Use an instance of the class ObjectGraphUtils which is in the ORMSupportClasses, and call the method DetermineActionQueues. (based on what you want to save: an entity or an entity collection).

It will calculate the complete insert/update queues for you. Then, traverse this queue and simply call a method you added to the entity, in which you've placed the code you now have in the OnBeforeEntitySave.

In there you can add entities to the entity's collections.

After that, simply call the save method on the adapter, and as it in there will recalculcate the queues to save, it will also save the entities you added in the routine you called when you traversed the queues you calculated yourself.

Now, you can automate this as this is fragile. So you can derive a class from DataAccessAdapter and in there override SaveEntity(SaveEntity(IEntity2 entityToSave, bool refetchAfterSave, IPredicateExpression updateRestriction, bool recurse).

In that override, perform the objectgraphutils routine, and then simply call base.SaveEntity(SaveEntity(IEntity2 entityToSave, bool refetchAfterSave, IPredicateExpression updateRestriction, bool recurse).

Use the same route for SaveEntityCollection. As SaveEntity and SaveEntityCollection aren't called recursively anymore, this is straight forward.

Frans Bouma | Lead developer LLBLGen Pro
CowHills
User
Posts: 47
Joined: 14-Mar-2007
# Posted on: 27-Feb-2010 08:17:36   

Hi,

I've been reading this post and I understand more or less what you are saying but I'm having trouble implementing the feature. Could you post some example code.

Many thanks,

Andre

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 28-Feb-2010 10:40:51   

Hi Andre,

What feature exactly are you implementing? and What do you have so far?

David Elizondo | LLBLGen Support Team
CowHills
User
Posts: 47
Joined: 14-Mar-2007
# Posted on: 02-Mar-2010 11:15:06   

Hi David,

I've been implementing the virtual method "OnBeforeEntitySave". I'm trying to add related entities in this method. Adding those related child entities is working fine but saving isn't working. The reason why this isn't working because the queue persister has already calculated the total size of the queue to save.

My main question is: Is it possible to add related entities and successfully save them.

Cheers,

Andre

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 02-Mar-2010 11:31:17   

Best place to add last minute changes to releted entities is: AuditInsertOfNewEntity of an Auditor.

Please next time, don't resurrect old threads.