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

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

    A simple solution if you are using the Entity Framework is the add a partical class and define a constructor for the entity as the framework does not define one. For example if you have an entity named Example you would put the following code in a seperate file.

    namespace EntityExample
    {
        public partial class Example : EntityObject
        {
            public Example()
            {
                // Initialize certain default values here.
                this._DateCreated = DateTime.Now;
            }
        }
    }
    

提交回复
热议问题