Tracing an in memory entity through entityCollection.

Posts   
 
    
Yash
User
Posts: 20
Joined: 07-Jan-2010
# Posted on: 19-Jan-2010 13:55:02   

Hello,

I am working with a static myEntityCollection, make changes through my application to the same (in memory only) and using SaveMulti() when required. Can you please suggest how to handle a case: myChild entity with FK reference tied to myParent entity that is having auto increment in DB as PK.

Now I create a new myParent entity(in memory only) that has no primary key generated till now (in DB Sql 2005). I need to bind that to a new myChild record (myChild is also in memory only till now), how can I figure out the actual PK of myParent to be tied to myChild while saving. Any help?

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 19-Jan-2010 16:18:06   

You don't need to - LLBLGen handles all of this for you. Just add the child entity to the collection of child entites on the parent entity, and save the parent.

LLBLGen will ensure that all of the saves happen in the correct order and that all of the Fk-Pks get synchronised correctly.

Matt

Yash
User
Posts: 20
Joined: 07-Jan-2010
# Posted on: 20-Jan-2010 07:25:11   

Matt earlier I was under same impression, but the problem arises when a parent entity resides in the memory itself and not committed to the DB and in between any child is added to that parent. All the entries are getting saved except the FK field( I am getting null) of child that had an IN MEMORY PARENT. Am I missing something here?

MTrinder wrote:

You don't need to - LLBLGen handles all of this for you. Just add the child entity to the collection of child entites on... Matt

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 20-Jan-2010 08:15:04   

Maybe you are doing something wrong in code. Would you please post a code snippet of how you associate a child entity to a parent entitiy, and then how you save them to the database.

The following code should save a Cutomer and its order:

var customer = new CustomerEntity();
customer.Name="John";
var order = new OrderEntity();
order.Quantity = 10;
customer.Orders.Add(Order);
customer.Save(true);//recursive save
Yash
User
Posts: 20
Joined: 07-Jan-2010
# Posted on: 20-Jan-2010 08:35:09   

Thanks for the code. Actually I have a tree structure with number of nodes as parent and child. User can add\edit\delete any of its type and I am using static entityCollections for my app and make changes (in memory only) and use SaveMulti() when required. So don't have a defined steps to add child or parent entity(so unable to post the code also) and this can be at random order. That's why child entities are left with having no parent ID, though getting saved in DB (as nulls too allowed). Am I able to explain my problem?

Walaa wrote:

Maybe you are doing something wrong in code. Would you please post a code ...

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 20-Jan-2010 08:55:49   

You are explaining your issue clearly, but without code we might not be able to help you.

Nevemind about your application complexity, just try reproducing it with simple code. Use something like the code I've posted. And see if the FK gets saved correctly. If not then post the repro code over here.