Excluded fields and NULL

Posts   
 
    
obzekt
User
Posts: 60
Joined: 29-Apr-2004
# Posted on: 29-Dec-2016 15:37:04   

Say you have fetched an entity without one of its nullable fields, and later you update that field conditionally. For example:


var coll = new EntityCollection<PersonEntity>();
adapter.FetchEntityCollection(coll, ... new ExcludedFieldList(PersonFields.Photo));
...
foreach (var e in coll)
{
     e.Name = "Joe";
     ...
     if (some condition)
     {
           object photo = GetPhoto();
           e.SetNewFieldValue((int)PersonFieldIndex.Photo, photo);
     }
}
adapter.SaveEntityCollection(coll);

We noticed that if photo==NULL, then the field is not updated. Otherwise it does. Is that by design? It seems inconsistent, and forces us to either fetch all the fields (which is redundant), or rewrite the code to update the entities directly (which is more convoluted).

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39612
Joined: 17-Aug-2003
# Posted on: 30-Dec-2016 11:07:08   

I think this is a side effect of the feature, sadly. The field is excluded and for the entity its value is 'null'. When you set it to null explicitly, the value actually doesn't change (null stays null) and the field isn't marked as changed.

There are a couple of ways around this, the easiest is this:

instead of:

e.SetNewFieldValue((int)PersonFieldIndex.Photo, photo);

do:

e.Fields.SetCurrentValue((int)PersonFieldIndex.Photo, photo);

This will always set the changed flag and should make the field 'dirty', causing it to be picked up. It also bypasses checks whether the value is the same, so it will always set it to a new value in the DB issuing an update query.

Frans Bouma | Lead developer LLBLGen Pro
obzekt
User
Posts: 60
Joined: 29-Apr-2004
# Posted on: 30-Dec-2016 16:09:24   

Thanks for the tip! And happy new year..

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39612
Joined: 17-Aug-2003
# Posted on: 30-Dec-2016 16:14:16   

obzekt wrote:

Thanks for the tip! And happy new year..

You too! simple_smile

Frans Bouma | Lead developer LLBLGen Pro