SetExpression and IsDirty/IsChanged

Posts   
 
    
Findev
User
Posts: 107
Joined: 08-Dec-2014
# Posted on: 21-Apr-2020 20:04:24   

Hi,

LinqPad 5, LLBLGen v.5.6x

Noticed that if I do:


var e = new XYZ();
e.Fields[(int)XYZFieldIndex.DateValidFrom].SetExpression(GetUtcDateTimeOffset());
this.AdapterToUse.SaveEntity(e).Dump("isSaveOk");


where

GetUtcDateTimeOffset()

is


public static DbFunctionCall GetUtcDateTimeOffset()
{
    return new DbFunctionCall("SYSDATETIMEOFFSET", null);
}

then it prints true, but no actual query is executed. Seems like .SetExpression is not triggering entity to be "dirty", to fix this I either have to set some other property to some value then .SetExpression starts working or explicitly mark that field as changed:

e.Fields[(int)XYZFieldIndex.DateValidFrom].IsChanged = true;

Is it by design that it: if some other property is modified then .SetExpression field(s) don't need explicit

.IsChange = true

if only .SetExpression is used, then it requires explicit

.IsChange = true

or for entity

e.IsDirty = true

Thank you!

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 22-Apr-2020 07:43:54   

Hi Findev,

Findev wrote:

[code] var e = new XYZ(); e.Fields[(int)XYZFieldIndex.DateValidFrom].SetExpression(GetUtcDateTimeOffset()); this.AdapterToUse.SaveEntity(e).Dump("isSaveOk");

This entity is new, Right? And you are only setting a field which is an expression, Right?

Findev wrote:

Is it by design that it: if some other property is modified then .SetExpression field(s) don't need explicit

.IsChange = true

if only .SetExpression is used, then it requires explicit

.IsChange = true

Yes, this is expected, as you are not really modifying the entity, so it wouldn't be noticed as a change/modification.

David Elizondo | LLBLGen Support Team
Findev
User
Posts: 107
Joined: 08-Dec-2014
# Posted on: 22-Apr-2020 08:45:11   

It's the same behavior if

e.IsNew = false;

Out of curiosity: why .SetExpression() doesn't trigger the change? In the end if some entity's property is modified or entity is marked as dirty then previously set expression still takes place meaning it is being tracked anyways.

Thank you!

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39749
Joined: 17-Aug-2003
# Posted on: 22-Apr-2020 10:41:13   

The persistence engine of entity instances checks for whether an entity is dirty or whether an entity becomes dirty after a save of a related entity (because one of its fk fields gets a value synced). It doesn't check whether an expression is set on a field.

When an entity is dirty at the time of persisting it, it will be handed off to the query engine and at that moment it will be checked whether a field has an expression set.

The actual use case to do what you want is to use the UpdateEntitiesDirectly method: the entity passed in is expected to have fields set or expressions set on its fields and is passed as-is to the query engine: there the same code as the one which accepts an entity with changed fields, is handling it and producing an update query.

So if you want to have fields set based on an expression, you need to use the UpdateEntitiesDirectly method. The idea is that the entity you set the expression on isn't the only one changing hence the requirement to do it this way. That you can set an expression on a field in an entity is more a side effect of how the low level api works. In general you wouldn't use it this way but only through the UpdateEntitiesDirectly method.

Frans Bouma | Lead developer LLBLGen Pro
Findev
User
Posts: 107
Joined: 08-Dec-2014
# Posted on: 22-Apr-2020 12:03:24   

Thank you for your response.

5-6 years ago I had snippets which set the .IsChanged to true. Later on, some of those where refactored (+removed IsChanged assignments) and the result was still the same, yet, if I remember correctly originally I had to set the changed flag to get the desired result. Of course during the refactoring other things got updated too, thus can't say whether the run-time has changed that behavior or, most probably, my own refactoring side-effect simple_smile Speaking of which: after the explanation I understand why you call it like that, but great it exists. For the task at hand I'm saving a new entity and certain fields require some date and time arithmetic using db functions.

Thanks again! simple_smile