Question

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.

Was it helpful?

Solution 2

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top