代码首先,在DateTime2字段上设置GetDate()

时间:2019-06-02 11:08:07

标签: entity-framework-6 ef-code-first getdate datetime2

我遵循了此处接受的示例:Possible to default DateTime field to GETDATE() with Entity Framework Migrations?

在Sql服务器中,“默认绑定或值”现在已设置为('1900-01-01T00:00:00.000'),但没有达到我期望的GetDate()。

public partial class InitialCreate : DbMigration
{
    public override void Up()
    {
        AddColumn("dbo.UserWordsModels", "DateAdded", c => c.DateTime(defaultValueSql: "GETDATE()"));

    }

    public override void Down()
    {
        DropColumn("dbo.UserWordsModels", "DateAdded");
    }
}

在模型中:

[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public DateTime DateAdded { get; set; }

我可能有所不同的一件事是我设置了这个,所以所有的DateTime字段都是DateTime2

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Properties<DateTime>().Configure(c => c.HasColumnType("datetime2"));}

0 个答案:

没有答案