Adding CreatedDate to an entity using Entity Framework 5 Code First

后端 未结 7 502
心在旅途
心在旅途 2020-12-13 02:24

I am trying to add a CreatedDate property to entities in my Model and am using EF5 Code First. I want this date to not be changed once set, I want it to be a UT

7条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-13 03:16

    Here is how I did it:

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

    in my migration's Up() method:

    AddColumn("Agents", "CreatedDate", n => n.DateTime(nullable: false, defaultValueSql: "GETUTCDATE()"));
    

提交回复
热议问题