How can NHibernate use the default database value when saving a new object?

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; }
}

Displayed in the following simple NHibernate display:

<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 Entityand saving to the database, how do you instruct NHibernate to use the default database value for the column DateCreatedif its value is equal null? Alternatively, how can I indicate that NHibernate should use the result of the function getUtcDate()as the value for the field DateCreatedwhen pasting?

So far I could easily add

DateCreated = DateTime.Now;

Entity, , , , .

+5
2

, :

NHibernate Mapping -

, :

generated="insert"

, NHibernate INSERT, , DateCreated .

:

update="false" insert="false"

, , NHibernate .

+4

. dynamic-insert="true" .
:

dynamic-insert (optional - defaults to false): specifies that INSERT SQL should be generated at runtime and contain only the columns whose values are not null.
+1

All Articles