Code-first migration: How to set default value for new property?

Solution 1:

If you see the generated migration code you will see AddColumn

AddColumn("dbo.report", "newProperty", c => c.String(nullable: false));

You can add defaultValue

AddColumn("dbo.report", "newProperty", 
           c => c.String(nullable: false, defaultValue: "old"));

Or add defaultValueSql

AddColumn("dbo.report", "newProperty",
           c => c.String(nullable: false, defaultValueSql: "GETDATE()"));

Solution 2:

Hope it helps someone. Putting everything together from previous answers (example using a boolean property):

1) Add a new property to the entity.

/// <summary>
/// Determines if user is enabled or not. Default value is true
/// </summary>
public bool IsEnabled { get; set; }

2) Run the command below to add the new change in the migrations.

add-migration addIsEnabledColumn

3) A migration file is created from the command above, open that file.

enter image description here

4) Set the default value.

public override void Up()
{
        AddColumn(
            "dbo.AspNetUsers", 
            "IsEnabled", 
            c => c.Boolean(
                nullable: false, 
                defaultValue: true
            )
        );
}

Solution 3:

You have to change the line in your migration script which adds the the property/column like this:

AddColumn("dbo.reports", "newProperty", c => c.String(nullable: false, defaultValue: "test"));