Allowing nulls for updates

Posts   
 
    
worldspawn avatar
worldspawn
User
Posts: 321
Joined: 26-Aug-2006
# Posted on: 15-Sep-2010 06:31:40   

Hi there.

LLBL 3.0.

I'm mapping models to entities via automapper. A complaint I have received is that null values are not being persisted to the database (for an UPDATE scenario). I can see that I need to manually change IsChanged on the field to true when I am setting a value to null.

I can tack on extra instructions to automapper, but when the fields are the same name and same type I don't want to have to create a definition for each field to manage this.

So I came up with this (added to the entity definition).


protected override void OnSetValueComplete(int fieldIndex)
        {
            if (this.Fields[fieldIndex].CurrentValue == null &&
                !this.Fields[fieldIndex].IsChanged)
            {
                this.Fields.IsDirty = true;
                this.Fields[fieldIndex].IsChanged = true;
            }
            base.OnSetValueComplete(fieldIndex);
        }

I was wondering if this would create any issues? I'm using this in a asp.net application, never for windows.

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 15-Sep-2010 09:16:04   

Are you fetching the entity before updating it? If so what's the DBValue of the field? Would you please post the update code?

worldspawn avatar
worldspawn
User
Posts: 321
Joined: 26-Aug-2006
# Posted on: 16-Sep-2010 08:34:33   

No I'm not fetching the entity before updating it.

I can't post the update code as there is none. AutoMapper pushes the values for my model class to the entity class.

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 16-Sep-2010 10:13:40   

What you have done is correct.