Setting the default value of a DateTime Property to DateTime.Now inside the System.ComponentModel Default Value Attrbute

后端 未结 21 1346
终归单人心
终归单人心 2020-11-29 22:53

Does any one know how I can specify the Default value for a DateTime property using the System.ComponentModel DefaultValue Attribute?

for example I try this:

相关标签:
21条回答
  • 2020-11-29 23:26

    I think the easiest solution is to set

    Created DATETIME2 NOT NULL DEFAULT GETDATE()
    

    in column declaration and in VS2010 EntityModel designer set corresponding column property StoreGeneratedPattern = Computed.

    0 讨论(0)
  • 2020-11-29 23:29

    Add below to the DateTime property

    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    
    0 讨论(0)
  • 2020-11-29 23:30

    Just found this looking for something different, but in the new C# version, you can use an even shorter version for that:

    public DateTime DateCreated { get; set; } = DateTime.Now;
    
    0 讨论(0)
提交回复
热议问题