views:

318

answers:

2

Several of our database tables contain LastModifiedDate columns. We would like these to stay synchronized based on a single time-source. Our best time-source, in this case, is the SQL Server itself since there is only one database server but multiple application servers which could potentially be off sync.

I would like to be able to use NHibernate, but have it use either GETUTCDATE() or DEFAULT for the column value when updating or inserting rows on these tables.

Thoughts?

Edit: Based on the lack of responses, I simply have to believe that this is something that NHibernate is just not capable of doing. This makes me sad.

+2  A: 

When mapping the class property to the table column, set insert="false" update="false".

Nuno G
That prevents it from setting the row on INSERT and UPDATE all together. For inserts the DEFAULT value works. But for an update... I want it to actually send "DEFAULT" or "GETUTCDATE()" as the parameter. Otherwise I'll need to employ database triggers, and I want to avoid where I can.
Joseph Daigle
If you are using MS-SQL server, you don't need a trigger. Modify the database table and in the column attributes, set default="Getutcdate()".
Nuno G
But that only works on INSERT statements. SQL won't use the default value for an UPDATE unless you set the value as the keyword DEFAULT into your statement. NHibernate won't set this value.
Joseph Daigle
Good point. I am afraid you can only do it using triggers.
Nuno G
Avoiding triggers when they are clearly the correct tool for the task is short-sighted. Triggers are a good thing for certain tasks like this, not a bad thing.
HLGEM
I think it's debatable whether the trigger is correct tool. If you have multiple applications and you're not sure if they're all going to follow the rules, then yes, a trigger is the correct option. But if you control the entire stack, including what accesses the database, then it is sufficient to simply set the value as DEFAULT whenever you do an UPDATE.
Joseph Daigle
A: 

Since you want to use a server function for BOTH insert and update, and you probably want to keep that consistent in memory, I suggest that you use a trigger and then set:

insert="false" update="false" generated="always".

That way, NHibernate will retrieve the inserted/updated value from the DB whenever you save.

Diego Mijelshon