Possible to default DateTime field to GETDATE() with Entity Framework Migrations?

前端 未结 7 979
轻奢々
轻奢々 2020-11-28 10:20

I added EntityFramework.Migrations (Beta 1) to an existing Code-First app that is going through some changes (for both migration capabilities and more fine-tuning of the tab

7条回答
  •  庸人自扰
    2020-11-28 10:54

    Create a migration:

    public partial class Table_Alter : DbMigration
    {
        public override void Up()
        {
            AddColumn("dbo.tableName", "columnName", 
               c => c.DateTime(nullable: false, defaultValueSql: "GETDATE()"));
        }
    
        public override void Down()
        {
            DropColumn("dbo.tableName", "columnName");
        }
    }
    

    For existing records it will set the datetime when you will run Update-Database command, for new records it will be set the datetime of creation

提交回复
热议问题