Fluent migrator. How to add datetime column with default values as current date?
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.
You probably have already found the documentation for SystemMethods on the wiki. I just updated it so that it reflects the latest version of FluentMigrator.
Just want to point out that these are database-specific and only Sql Server has all five SystemMethods implemented. This makes your migrations less portable as it is no longer standard sql that is generated and some of the SystemMethods are not supported for other databases (CurrentUser does not seem to be possible in MySql for example).
If you see the need for any others then please log it as an issue on FluentMigrator's Github site and we'll try and add it in.