Hi lingers,
You need to override the Up and Down method.
First add the column with a default value, then drop the default value.
protected override void Up(MigrationBuilder migrationBuilder)
{
// Add the column with a default value, then drop the default value.
// This creates a non-nullable column without the migration failing because of existing data.
migrationBuilder.AddColumn<int>(
name: "ColumnName",
table: "TableName",
type: "int",
nullable: false,
defaultValue: 1); // Default value
migrationBuilder.AlterColumn<int>(
name: "ColumnName",
table: "TableName",
oldDefaultValue: 1,
defaultValue: null);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "ColumnName",
table: "TableName");
}
Reference:
https://stackoverflow.com/questions/18569003/entity-framework-code-first-making-a-column-non-nullable