Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FluentNHibernate - Setting default value for DB columns (SQL Server)

does anyone know a way how I could set through mapping the default value of a column so for e.g. when I generate DB from mappings I would have DateTime column having getdate() as default value?

I tried so far this (looks exactlly like what I need) but it doesn't work

this.Map(x => x.LastPersistedOn, "DateModified") 
    .Access.Property() 
    .Default("getdate()"); 
like image 241
Nikola Malovic Avatar asked Oct 15 '09 11:10

Nikola Malovic


1 Answers

I just tried setting some default values and it worked as expected. I am using Fluent as retrieve from Git the 24.05.2010 so updating your copy may solve your problem.
Mapping

public class SampleEntity
{
    public virtual DateTime DateTimeProperty { get; set; }
}

With

 public class SampleEntityMap
        : ClassMap<SampleEntity>
{
   public SampleEntityMap()
   {
    Map(x => x.DateTimeProperty, "DateTimeColumn")
             .Access.Property()  //actually not necessary 
             .Not.Nullable()
             .Default("getDate()");
   }
}

this will produce the following sql (from output to the console)

create table SampleEntity(
   DateTimeColumn DATETIME default  getDate()  not null
  )

--
Dom

like image 189
Dom Ribaut Avatar answered Oct 21 '22 15:10

Dom Ribaut