第一次迁移:如何为新的属性设置默认值?
  我正在使用EF6在我的数据库中存储report类的实例。  该数据库已经包含数据。  假设我想添加一个属性来report , 
public class report {
    // ... some previous properties
    // ... new property:
    public string newProperty{ get; set; }
}
现在,如果我去包管理器控制台并执行
add-migration Report-added-newProperty
update-database
  我将在'/ Migrations'文件夹中为文件添加一个newProperty列到表中。  这工作正常。  但是,在数据库中较旧的条目中, newProperty的值现在是一个空字符串。  但我希望它是,例如,“老”。 
所以我的问题是:如何为迁移脚本(或其他地方)中的新属性(任何类型)设置默认值?
  如果您看到生成的迁移代码,您将看到AddColumn 
AddColumn("dbo.report", "newProperty", c => c.String(nullable: false));
  您可以添加defaultValue 
AddColumn("dbo.report", "newProperty", 
           c => c.String(nullable: false, defaultValue: "old"));
  或者添加defaultValueSql 
AddColumn("dbo.report", "newProperty",
           c => c.String(nullable: false, defaultValueSql: "GETDATE()"));
您必须在您的迁移脚本中更改行,并添加属性/列,如下所示:
AddColumn("dbo.reports", "newProperty", c => c.String(nullable: false, defaultValue: "test"));
我发现只要在实体属性上使用Auto-Property Initializer就足以完成工作。
例如:
public class Thing {
    public bool IsBigThing { get; set; } = false;
}
上一篇: first migration: How to set default value for new property?
