SubSonic 2.1: Some Updated Fields Not Being Marked As Dirty
Added Saturday, October 18, 2008 by Thevin Sattayatam, Developer · No Comments
If you’re a .NET developer and not familiar with SubSonic, you should take a look at http://subsonicproject.com/ to read up about this great open-source object relational mapping (ORM) tool. Of course, using a ORM tool isn’t the most efficient way to move data between your application and the database. But using it will greatly reduce your development time and the hit on the performance is negliable if used correctly.
The latest released of SubSonic is version 2.1 (released July 8, 2008). It is still a working progress with several bugs to work out. One of those bugs in version 2.1 has to do with updating records with the new value being the default value for that field. This behavior will happen if it meets the following conditions:
- The object/record being updated wasn’t directly loaded from the database.
- The object’s state (isLoaded & isNew) are incorrectly set when new values are being assigned to it.
- The new value assigned is equal to the default value for the field. (Does not matter if it’s determined by the database or SubSonic’s default value logic).
Say you have a bit field that the database defaults to false and is not nullable or simply marked as not nullable and SubSonic assign a non-nullable boolean type for the field which defaults to false. In our example below, in the database the value of “BitFlag” for this particular record is actually true and we want to update it and and set “BigFlag” to false.
// The bject not loaded from directly from the database
Article ArticleObject = new Article();
// Article.BitFlag is defaulted to false when it is initialized
ArticleObject.Id = 1;
ArticleObject.BitFlag = false;
ArticleObject.Update();
The issue is that we’re not reloading the object from the database prior to updating it (which would have “BitFlag” equaling to true) and the old value of the field is false by default and the new value being assigned to it is also false. SubSonic thinks that value of the field has not changed, so the “BitFlag” field does not get added to the DirtyColumns collection.
I created a patch which it can be downloaded here: dirtydefaultvalue.patch
What the patch does:
Added a boolean flag to determine if the field contains the default value. Upon calling SetColumnValue, it check if the current value differs from the new value and also checks if the new value is equal to the current value and current value is the default value. If either of the condtions evaluate to true, then the DirtyColumns collection gets updated.
