Saving Individual Entities or as a Collection

Posts   
 
    
Kilan
User
Posts: 6
Joined: 16-Jan-2013
# Posted on: 16-Feb-2013 18:39:24   

Hi,

I am having a problem updating/saving of multiple entities of two related (FK-PK) types, however, before describing it, I have gotten it to work by saving each entity individually first rather than in bulk as a collection of such entity.

So, before I attempt to understand the issue, let me ask, is there a performance penalty to do a DML operation in a loop on multiple entities individually, or should I add them to the respective collection type, then do SaveMulti()?

Loop:
  Modify/Create entity
  Save()

or

Loop:
  Modify/Create entity
  Add entity to collection
End Loop

Collection.SaveMulti()

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 18-Feb-2013 00:29:02   

There is no penalty in saving entities individually versus saving a collection, as when you save a collection, inside is saved each entity individually. The advantage with the collection is that a single connection is used, and a transaction is started internally, so it'a an atomic save.

When you save entities individually, a new connection is opened each time, and there is no implicit transaction used. However you can manipulate the connection and start a transaction (ref...:

var tx = new Transaction(IsolationLevel.Serializable,  "tx");
Loop:
tx.Add(entity);
Modify/Create entity
Save()
EndLoop
tx.Commit();
David Elizondo | LLBLGen Support Team