Fluent migrator. How to add datetime column with default values as current date?

Seth picture Seth · Jan 5, 2013 · Viewed 8.2k times · Source

How can I alter column in fluent migration with default date time value that is current date time?

So I need smth like this:

ALTER TABLE database ADD column DATETIME DEFAULT GETDATE() NOT NULL;

only for fluent migration. Thanks.

Answer

Chris Marisic picture Chris Marisic · Mar 18, 2016

Seeing as the answer doesn't actually include any code:

Create.Table(nameof(Report))
      .WithColumn(nameof(Report.Id)).AsInt32().NotNullable().PrimaryKey().Identity()
      .WithColumn(nameof(Report.CreatedAt)).AsDateTime().Nullable()
                                           .WithDefault(SystemMethods.CurrentDateTime);

The WithDefault(SystemMethods) method is the solution.