Dirty fields and nullable types

Posts   
 
    
worldspawn avatar
worldspawn
User
Posts: 321
Joined: 26-Aug-2006
# Posted on: 31-May-2009 03:52:40   

Hi,

I'd always wondered what would happen if you created an entity marked IsNew = false, set the primary key and then only set 1 or 2 properties. What would happen, would those 1 or 2 properties be set and everything else made to be null. The answer turned out to be no, it only updates the fields you set. This is awesome! Many a time to projects past I have loaded the entity, set a couple of fields and saved it back to the DB... oops!

However I ran into a bit of hiccup with this when working with nullable typed fields.


var siteCheckListItem = new SiteCheckListItemEntity();
                                siteCheckListItem.SiteOwnershipId = (int)vreSite.CurrentOwnershipId;
                                siteCheckListItem.CheckListItemId = checkListItem.CheckListItemId;
                                siteCheckListItem.IsNew = false;
                                siteCheckListItem.IsDirty = true;
                                if (checkListItemId != null && checkListItemId.Contains(checkListItem.CheckListItemId))
                                {
                                    siteCheckListItem.IsComplete = true;
                                    siteCheckListItem.DateCompleted = DateTime.Now;
                                    siteCheckListItem.MarkedCompleteById = CurrentUserId;
                                }
                                else
                                {
                                    siteCheckListItem.IsComplete = false;
                                    siteCheckListItem.DateCompleted = null;
                                    siteCheckListItem.MarkedCompleteById = null;
                                }

When the "else" branch executes it sets DateCompleted and MarkedCompleteById to null however LLBL seems to consider that a no-op as it sets IsComplete to false but ignores the other two field changes. I've found that doing this:



                                    siteCheckListItem.Fields["DateCompleted"].IsChanged = true;
                                    siteCheckListItem.Fields["MarkedCompleteById"].IsChanged = true;

forces LLBL to includes those fields in the update statement. However... I don't like this much. Is there an alternative that doesn't leave me writing an extra line of code for each property I set?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 31-May-2009 20:00:16   

Is there an alternative that doesn't leave me writing an extra line of code for each property I set?

No simple_smile

The thing is that your entity isn't fetched, so the original value (DBValue) is null, then you set the value to null, LLBLGen doesn't detect any change in that action, so, for performance the field is not included in the update list. LLBLGen doesn't know if the entity is fetched or not. In brief, you have to do what you are doing (forcing the change).

David Elizondo | LLBLGen Support Team
worldspawn avatar
worldspawn
User
Posts: 321
Joined: 26-Aug-2006
# Posted on: 01-Jun-2009 02:43:45   

Ok thanks Daelmo