Creating and then modifying entity collection before saving

Posts   
 
    
AlbertK
User
Posts: 44
Joined: 23-Dec-2009
# Posted on: 22-Oct-2012 21:56:41   

Hello, I have a parent and child table in the database. FKs are not defined, so I added them in the model only.

I have parsing logic that manually manually constructs the parent entity, child entity and adds child entities to parent's collection, like parent.Children.Add(entity), where "Children" is EntityCollection<T>.

Unfortunately, PKs on the legacy tables are not identities, so I have to construct PK values before saving. I wanted to do this right before the save operation.

Is it possible to modify values of child entities once they've already been added to a parent entity's collection? I've tried iterating the child entity collection, but either get errors indicating collection cannot be modified or not seeing entity objects being modified.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 23-Oct-2012 05:39:32   

Hi Albert,

If you have a PK-FK defined (it doesn't matter if that is just model-only), LLBLGen use that relation information to do PK-FK synchronization, which means that if you set up the parent's pk, you don't need to worry about setting it on the children.

Now, sometimes you can't modify somethings on the children in a foreach loop because .net won't allow you because that property that you are changing affects the ownership of the parent over that child. If you are sure about what are you modifying then use a normal for(.;.;.) loop.

David Elizondo | LLBLGen Support Team