Consider the following simple C# class:
public class Entity
{
public Entity() { }
public virtual int Id { get; private set; }
public virtual DateTime DateCreated { get; private set; }
}
Mapped with the following simple NHibernate mapping:
<class name="Entity" mutable="false">
<id name="Id">
<generator class="native">
</id>
<property name="DateCreated"/>
</class>
To the following simple database schema:
CREATE TABLE Entity (
Id int IDENTITY(1,1) PRIMARY KEY,
DateCreated datetime NOT NULL DEFAULT getUtcDate()
)
When creating a new instance of the Entity and saving to the database, how do you instruct NHibernate to use the database's default value for the DateCreated column if its value is null? Alternatively, how can I specify that NHibernate should use the result of the getUtcDate() function as the value for the DateCreated field upon insertion?
While I could easily add
DateCreated = DateTime.Now;
into the Entity constructor, this is using the application server's local clock, and I need to use the database's local clock to ensure consistency when there are multiple application servers each with their potentially non-synchronized local clocks.