1
Vote

MS SQL Server insert/update of Timestamp columns

description

In evaluating the latest IQToolkit, I've run into a couple of issues related to inserting and updating of tables that have timestamp columns. (We utilize timestamp columns in our tables for concurrency checking.)
 
When inserting a new row, the executed SQL attempts to insert a value (NULL by default) into the timestamp column. I was able to resolve the issue by adding "IsGenerated = true" to the column attribute. I had hoped that the same attribute change would also work for an update, but it doesn't. An update statement against a table with a timestamp column will attempt to set it's value. Of course this is not allowed by MS SQL Server and results in a run-time error.
 
After tracing the code I found that the only stipulation that an Update statement makes for column inclusion is that it the mapping function IsUpdatable() must return true for the column. In looking at the IsUpdatable() function, it is defined as part of the BasicMapping class and not overridden anywhere up the inheritance chain (at least that I could find). The IsUpdatable() function only requires that the column not be a primary key. It does not seem to honor the IsGenerated attribute. I have added code to check for IsGenerated as well in my local copy of the class that seems to resolve my issue (see code below). However, I do not know yet if there are any potential negative side effects.
 
Is there a better way to handle this situation without code modifications? I could have inherited from AttributeMapping and overridden the IsUpdatable() method, but I thought this might be more a bug or oversight than intentional.
 
public virtual bool IsUpdatable(MappingEntity entity, MemberInfo member)
{
 //return !this.IsPrimaryKey(entity, member);
 return !(this.IsPrimaryKey(entity, member) || this.IsGenerated(entity, member));
}
 
This change simply adds a requirement that the column cannot be

comments

mattwar wrote Apr 20, 2012 at 11:49 PM

IsGenerated is only supposed to matter for inserts not updates. However, IsComputed is supposed to matter for both. I'm changing IsUpdatable to consider computed columns to not be updatable. This should fix the problem if you declare the timestamp column as computed.