Add entity to collection without retrieving collection

Posts   
 
    
Posts: 1268
Joined: 10-Mar-2006
# Posted on: 14-Sep-2006 21:43:12   

When you do the following in selfservicing:


MyEntity myEntity = new MyEntity();
MyCollectedEntity myCollectedEntity = new MyCollectedEntity();
myEntity.MyCollectedEntity.Add(myCollectedEntity);
myEntity.Save(true);

The MyCollectedEntity collection is fetched from the database.

How can I add an entity to a collection like the above where the existing collection data is not fetched?

I suppose I could just create a MyCollectedEntity and set its foreign key, etc, but that did not seem 'the right way' to do it. (Especially since I am changing properties in MyEntity at the same time)

Also, I tried creating my own collection and assigning that to MyCollectedEntity property - but the collection properties are read only.

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 15-Sep-2006 04:10:57   

This occurs because when you access myEntity.MyCollectedEntity the collection is lazy loaded. I don't know of a way to avoid this without just setting the FK directly in the entity.

Posts: 1268
Joined: 10-Mar-2006
# Posted on: 15-Sep-2006 04:28:14   

Yeah, I knew it was lazy loaded, but I do not want it to in this case. I coded it to do it setting the FK directly and my code is much more complex, and more obscure. (Plus I had to do a transaction, which was done for me before by save(true) )

Is this something that can be added to the already 'huge' todo/feature request list?

Potentially if you allowed me to assign the property directly, then I could have just created a collection and assigned it to it. However, preferably, I would just turn off lazy loading for that property, etc.

Thoughts Otis?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 15-Sep-2006 08:00:46   

MyEntity myEntity = new MyEntity(); MyCollectedEntity myCollectedEntity = new MyCollectedEntity(); myEntity.MyCollectedEntity.Add(myCollectedEntity); myEntity.Save(true);

First I want to know what you want to do with this code?

When an Entity X is related to a collection of Entities Y Then there is a FK in Y related to the PK of X

It seems from your code that you want to set all the entities of Y to have the same FK value that relates to an entity X.

This can be done by the following code:

// [C#]
YCollection Ycol = new YCollection();
YEntity YUpdate = new YEntity();
YUpdate.FK_to_X = xx; // you should save the X entity first then get the ID to be used here
Ycol.UpdateMulti(YUpdate, null, null);

Posts: 1268
Joined: 10-Mar-2006
# Posted on: 15-Sep-2006 16:12:02   

Just for an example, lets say you made a screen that simply let someone key in an additional line item to an invoice. Further, assume you loaded the InvoiceEntity so you could display basic invoice information (you did not load or display the other line items).

They hit save you write code:


myOrderEntity.LastUpdated=DateTime.Now;
myOrderEntity.Updatedby="Some User";
LineItemEntity myLineItem = new LineItemEntity();
myLineItem.Descrption="Whatever they entered";
myLineItem.Quantity=12;
myLineItem.Price=15.53;

myOrderEntity.LineItem.Add(myLineItem);
myOrderEntity.Save(true);


Note, this code above does exactly what you I want and works exactly as one would expect it to, with the exception of having a side effect of loading all the LineItemEntities into memory when they are not needed.

Granted there are other ways to accomplish this - however the above is the most intuitive and simple - so I just want to stop lazy loading. There already is a switch to force lazy loading to fetch EVERY time - this is the opposite of that switch.

Here is how I had to rewrite the above code:

Transaction transactionManager = new Transaction(IsolationLevel.ReadCommitted, "someTrans");
try
{
    myOrderEntity.LastUpdated=DateTime.Now;
    myOrderEntity.Updatedby="Some User";
    transactionManager.Add(myOrderEntity);
    myOrderEntity.Save();

    LineItemEntity myLineItem = new LineItemEntity();
    myLineItem.Descrption="Whatever they entered";
    myLineItem.Quantity=12;
    myLineItem.Price=15.53;
    myLineItem.OrderEntityId=myOrderEntity.OrderEntityId;

    transactionManager.Add(myLineItem);
    myLineItem.Save();

    transactionManager.Commit();
}
catch
{
    transactionManager.Rollback();
    throw;
}
finally
{
    transactionManager.Dispose();
}

I dont think there is any question about which code is best to read!

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 15-Sep-2006 17:30:59   

WayneBrantley wrote:

Just for an example, lets say you made a screen that simply let someone key in an additional line item to an invoice. Further, assume you loaded the InvoiceEntity so you could display basic invoice information (you did not load or display the other line items).

They hit save you write code:


myOrderEntity.LastUpdated=DateTime.Now;
myOrderEntity.Updatedby="Some User";
LineItemEntity myLineItem = new LineItemEntity();
myLineItem.Descrption="Whatever they entered";
myLineItem.Quantity=12;
myLineItem.Price=15.53;

myOrderEntity.LineItem.Add(myLineItem);
myOrderEntity.Save(true);


Note, this code above does exactly what you I want and works exactly as one would expect it to, with the exception of having a side effect of loading all the LineItemEntities into memory when they are not needed.

Granted there are other ways to accomplish this - however the above is the most intuitive and simple - so I just want to stop lazy loading. There already is a switch to force lazy loading to fetch EVERY time - this is the opposite of that switch.

Change: myOrderEntity.LineItem.Add(myLineItem);

into: myLineItem.Order = myOrder

done.

Frans Bouma | Lead developer LLBLGen Pro
Posts: 1268
Joined: 10-Mar-2006
# Posted on: 16-Sep-2006 02:41:08   

Change: myOrderEntity.LineItem.Add(myLineItem);

into: myLineItem.Order = myOrder

done.

Brilliant, I say, brilliant!

That should work great.....feature request implemented! smile