我尝试自动生成元数据列的默认值 在我的代码第一个数据库。似乎代码第一次迁移流畅的api无法处理 HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed)
我的数据库中的每个表都有一个" CREATION_DATE"列使用默认值" getdate()"。在我的EntityTypeConfiguration中,我尝试使用:
Property(p => p.CREATION_DATE).IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);
这是为迁移生成的:
CREATION_DATE = c.DateTime(nullable: false)
我可以手动添加defaultValueSql:" getdate()"但我不希望每次迁移都这样做。我为此编写了一个SqlServerMigrationSqlGenerator,但它似乎没有生成默认值。
internal sealed class Configuration : DbMigrationsConfiguration<TheModel>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
SetSqlGenerator(SqlProviderServices.ProviderInvariantName, new CustomMigrationSqlGenerator());
}
并在CustomMigrationSqlGenerator类中
protected override void Generate(CreateTableOperation createTableOperation)
{
SetDefaultDate(createTableOperation.Columns);
base.Generate(createTableOperation);
}
使用或不使用CustomMigrationSqlGenerator时不会生成默认值。
我错过了什么吗?我在论坛上看到它在某些时候是个问题,但这些帖子可以追溯到2010年至2012年。
*编辑
SetDefaultDate的内容
private static void SetDefaultDate(IEnumerable<ColumnModel> columns)
{
foreach (var item in columns)
{
SetDefaultDate(item);
}
}
private static void SetDefaultDate(ColumnModel column)
{
if (column.Type.Equals(System.Data.Entity.Core.Metadata.Edm.PrimitiveTypeKind.DateTime))
{
column.DefaultValueSql = "getdate()";
}
}