tags:

views:

344

answers:

4

Hi,

I am a newbie with NHibernate, so please bear with me.

Let's imagine, i have a property for CreatedDate, and i wanted it to be filled with the sql server datetime value.

The possible solution that i found out is, to mark this property as "generated=always" with "insert=false" and "update=false", and then set the default value for the CreatedDate on sql server level (i mean the database column), to "Getdate()".

Is this the right approach? Thanks for your time, any suggestion will be well appreciated.

+2  A: 

You need an nHibernate Audit Interceptor:

http://fgheysels.blogspot.com/2008/07/nhibernate-iinterceptor.html

Robert Harvey
Work great. Thanx
Daniel Mahadi
+1  A: 

Here's an example of exactly what you want from the NHibernate community site:

http://nhforge.org/wikis/howtonh/creating-an-audit-log-using-nhibernate-events.aspx

John Rayner
A: 

Another solution is to insert the date in SQLServer, when the record is created. Make the column non-nullable, and set its default value to the SQL functon GETDATE(). When the record is created, it will be date/timestamped. Map it in NHibernate like any other DateTime property.

David Veeneman
This is what we do, and we have an internal rule that any DateAdded and DateModified columns are done in this way. Simple and works.
Perhentian
A: 

Get the solution from John Rayner. Thanks for all that have helped. thanks for the time

Daniel Mahadi