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

后端 未结 21 1399
终归单人心
终归单人心 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:06

    I know this post is a little old, but a have a suggestion that may help some.

    I used an Enum to determine what to set in the attribute constructor.

    Property declaration :

    [DbProperty(initialValue: EInitialValue.DateTime_Now)]
    public DateTime CreationDate { get; set; }
    

    Property constructor :

    Public Class DbProperty Inherits System.Attribute
    
        Public Property InitialValue As Object
    
        Public Sub New(ByVal initialValue As EInitialValue)
           Select Case initialValue
              Case EInitialValue.DateTime_Now
                 Me.InitialValue = System.DateTime.Now
    
              Case EInitialValue.DateTime_Min
                 Me.InitialValue = System.DateTime.MinValue
    
              Case EInitialValue.DateTime_Max
                 Me.InitialValue = System.DateTime.MaxValue
    
           End Select
    
        End Sub
    End Class
    

    Enum :

    Public Enum EInitialValue
       DateTime_Now
       DateTime_Min
       DateTime_Max
    End Enum
    

提交回复
热议问题