Save method

Posts   
 
    
jbreuer
User
Posts: 43
Joined: 30-Nov-2009
# Posted on: 30-Dec-2009 13:54:06   

Hello,

I'm trying to save an entity using the following example I found at the documentation:

Option 2 is more efficient in that it just starts an update, without first reading the data from the database. The following code performs the same update as the previous example code illustrating option 1. Even though the PK field is changed, it is not updated, because it is not previously fetched from the database.

// [C#]
CustomerEntity customer = new CustomerEntity();
customer.CustomerID="CHOPS";
customer.IsNew=false;
customer.Phone = "(605)555-4321";
customer.Save();

We have to set the primary key field, so the Update method will only update a single entity, the "CHOPS" entity. Next, we have to mark the new, empty entity object as not being new, so Save() will call the Update method, instead of the Insert method. This is done by setting the flag IsNew to false. Next is the altering of a field, in this case "Phone", and the call of Save(). This will not load the entity back in memory, but because Save() is called, it will be marked out of sync, and the next time you'll access a property of this entity's object, it will be refetched from the persistent storage. Doing updates this way can be very efficient and you can use very complex update constructs when you apply an Expression to the field(s) to update.

The save method returns a boolean. What does this boolean mean? Is it true if data is inserted or updated and what happens if I want to update data, but no fields are changed? Is the boolean false if nothing is updated or inserted?

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 30-Dec-2009 14:15:25   

The save method returns a boolean. What does this boolean mean?

Is it true if data is inserted or updated and what happens if I want to update data, but no fields are changed?

No Update is issued if no fields were changed. It will only return false, if an Update was issued but no rows were affected.

jbreuer
User
Posts: 43
Joined: 30-Nov-2009
# Posted on: 30-Dec-2009 14:20:42   

Walaa wrote:

No Update is issued if no fields were changed. It will only return false, if an Update was issued but no rows were affected.

How does it know no fields were changed? No data was fetched yet so doesn't it just execute an Update without affecting any rows?

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 30-Dec-2009 15:03:42   

If you have set any field other than the PK, then the entity is Dirty and thus an Update will be issued.

jbreuer
User
Posts: 43
Joined: 30-Nov-2009
# Posted on: 30-Dec-2009 15:39:22   

Walaa wrote:

If you have set any field other than the PK, then the entity is Dirty and thus an Update will be issued.

Ok thanks that's good to know! This means if I set any field other than the PK but the value is still the same as in the database an update is issued, but no rows are updated and the save method returns false.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 30-Dec-2009 16:27:57   

Ok thanks that's good to know! This means if I set any field other than the PK but the value is still the same as in the database an update is issued, but no rows are updated and the save method returns false.

The row will be updated in this case.

jbreuer
User
Posts: 43
Joined: 30-Nov-2009
# Posted on: 30-Dec-2009 16:46:23   

Walaa wrote:

The row will be updated in this case.

Hmm so it's updated even though no data is actually changed. How can I make sure it's not updated? Do I need to fetch the data first?

An example:


ProductEntity prod = new ProductEntity(u4prod.ItemCode);
prod.Description = u4prod.ItemDescr;
bool saved = prod.Save();

If prod.Description is already the same as u4prod.ItemDescr it's not updated? Does prod.Save() returns false in that case?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 31-Dec-2009 04:11:37   

If the field values are the same as the original the entity isn't dirty so there's nothing to do, so the SaveEntity will return true and the save routine wont be sent to the DB.

You could check the IsDirty property to know whether the entity will be saved or not.

David Elizondo | LLBLGen Support Team
jbreuer
User
Posts: 43
Joined: 30-Nov-2009
# Posted on: 04-Jan-2010 08:53:39   

daelmo wrote:

You could check the IsDirty property to know whether the entity will be saved or not.

Ok thanks. So if I change my code to the following it should work?

ProductEntity prod = new ProductEntity(u4prod.ItemCode);
prod.Description = u4prod.ItemDescr;
if (prod.IsDirty)
{
prod.Save();
}

If u4prod.ItemDescr is already the same as prod.Description the value isn't changed and IsDirty should be false?

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

Yes your code is correct but still the if condition is not necessary, since anyway this check is made inside the Save method.

jbreuer
User
Posts: 43
Joined: 30-Nov-2009
# Posted on: 04-Jan-2010 09:06:00   

Walaa wrote:

Yes your code is correct but still the if condition is not necessary, since anyway this check is made inside the Save method.

The code above was just an example. In my situation if the the entity is dirty I need to execute some extra code. Thanks for helping me out!

jbreuer
User
Posts: 43
Joined: 30-Nov-2009
# Posted on: 05-Jan-2010 16:24:41   

I've got another question regarding to the save method. I've got 2 examples and I would like to know which would be faster.

Example 1 In this example IsNew is determined by a count query. After this an update query is always execute (even if no data was updated) because no data is fetched yet and IsDirty is always true. This code executes 2 lightweight queries.


ProductGroupEntity productGroupEntity = new ProductGroupEntity();
                productGroupEntity.IsNew = !FindProductGroup(productGroup.ProductGroupCode);
                productGroupEntity.Code = productGroup.ProductGroupCode;
                productGroupEntity.ParentCode = productGroup.ProductGroupCodeParent;
                productGroupEntity.Description = productGroup.ProductGroupCodeDescr;
                productGroupEntity.Nivo = productGroup.Nivo;
                productGroupEntity.Quantity = productGroup.Quantity;
                productGroupEntity.Save();

Example 2 In this example all the data is always fetched (if the row exists). IsNew is determined automatically and false if data is found. If some data is updated IsDirty will be true and the Save method will update the data. This code executes 1 query if no data is updated and 2 queries if data is updated.


ProductGroupEntity productGroupEntity = new ProductGroupEntity(key);
                productGroupEntity.Code = productGroup.ProductGroupCode;
                productGroupEntity.ParentCode = productGroup.ProductGroupCodeParent;
                productGroupEntity.Description = productGroup.ProductGroupCodeDescr;
                productGroupEntity.Nivo = productGroup.Nivo;
                productGroupEntity.Quantity = productGroup.Quantity;
                productGroupEntity.Save();

Which example has better performance? In my situation the data will only be updated rarely. Does this mean example 2 is faster since it will only execute 1 query in most situations?

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 05-Jan-2010 21:55:38   

As ever with these type of questions, there is only one sensible answer - measure it under expected conditions and see for yourself simple_smile

In theory the example with the fewer queries should be faster as these are usually the bottleneck, but you need to verify this for yourself.

Matt