Setting field to NULL do not set it in DB

Posts   
 
    
kievBug
User
Posts: 105
Joined: 09-Jan-2009
# Posted on: 30-May-2011 17:22:39   

Hi

I'm on llblgen 3.1.11.0208(Adapter). I'm trying to update entity without fetching it like this:


  var entity = new TestEntity("PKKEYVALUE");
  entity.IsNew = false;
  entity.NullableField = null;
  adapter.SaveEntity(entity);

After that the value of the field in the database is not changed. This happens because the entity wasn't fetched and you just assume that the current value of the field is null, but it is not null in the database.

Is this an expected behavior?

As for me it is a bug that needs to be fixed.

Thanks, Anton

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

Hi Anton,

That is the expected behavior. I'will try to explain: - Right after you instantiate the entity, you set IsNew=false, so the framework now think this is a FETCHED entity. - Next you set the nullable field to NULL. This field wont be marked as changed because from the framework point-of-view the entity is not new, so it compares the DBValue with CurrentValue and figure it out that there is nothing to change. - You save the entity. As the nullable field wasn't marked as changed, it wont be updated.

To overcome this, set the IsNew = false just before the save action. This way, when you set the nullable field to NULL, the framework will treat the entity as new, thus the field will be marked as changed.

var entity = new TestEntity("PKKEYVALUE");
entity.NullableField = null;

entity.IsNew = false;
adapter.SaveEntity(entity);

This is documented somewhere here.

David Elizondo | LLBLGen Support Team
kievBug
User
Posts: 105
Joined: 09-Jan-2009
# Posted on: 31-May-2011 15:09:19   

Ok. Thanks. Will give it a try.

Anton