update all related records

Posts   
 
    
chungpn
User
Posts: 39
Joined: 23-Mar-2011
# Posted on: 13-May-2011 04:16:57   

I have this code

CustomerEntity _currentCustomer=new CustomerEntity(); _currentCustomer.FetchUsingPK("ALFKI"); _currentCustomer.CompanyName=textBox1.Text ; _currentCustomer.ContactName= textBox2.Text ;

       _currentCustomer.Orders[0].Freight=(decimal)126;


            _currentCustomer.Save(true);

This code only update the record 0 of Orders table. How can I update all related records in the Orders table? Do I need loop all the items in the _currentCustomer.Orders collection to update?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 13-May-2011 06:06:06   

chungpn wrote:

This code only update the record 0 of Orders table.

Yes, because you are only updating that order.

chungpn wrote:

How can I update all related records in the Orders table? Do I need loop all the items in the _currentCustomer.Orders collection to update?

That is an option, as a matter of fact if you will set different Freight values for each order then this is the way to go.

Now, if you want to set 126 Freight value for all orders related to that customer in the DB, then it would be faster to Update all orders directly:

OrderCollection orders = new OrderCollection();
OrderEntity orderUpdate = new OrderEntity();
orderUpdate.Freight = 126;
IPredicateExpression selectFilter = new PredicateExpression(
     OrderFields.CustomerId ==_currentCustomer.CustomerId));

int amountRowsAffected = orders.UpdateMulti(orderUpdate, selectFilter, null);
David Elizondo | LLBLGen Support Team
chungpn
User
Posts: 39
Joined: 23-Mar-2011
# Posted on: 13-May-2011 08:40:57   

Thank you, daelmo. You are the man. But my question is in the Save method of _currentCustomer with the recurse parameter set to TRUE. How to make it save 126 Friegt to all the related item in Orders.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 13-May-2011 10:14:01   

Can't do it in the save of the customer unless you loop through all related orders. You may use a transaction to wrap the Customer Save action and the Orders UpdateMulti action aas well, thus if you want it to be atomic.

chungpn
User
Posts: 39
Joined: 23-Mar-2011
# Posted on: 13-May-2011 11:18:07   

Do that Customer Save action and the Orders UpdateMulti action already have their own transaction?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 13-May-2011 11:22:06   

If run separately, they would issue database commands, for each the database creates an implict transaction.

So if you want to run them in the same transaction, you should do this explicitly.