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.
You need an nHibernate Audit Interceptor:
http://fgheysels.blogspot.com/2008/07/nhibernate-iinterceptor.html
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.
You could map the property as a Datetime. Then, before any insert/update, you can get the date from database, like this:
myEntity.LastModifiedDate = Session.CreateSQLQuery("SELECT GETDATE()").UniqueResult<DateTime>();
Session.SaveOrUpdate(myEntity);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With