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

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

    How you deal with this at the moment depends on what model you are using Linq to SQL or EntityFramework?

    In L2S you can add

    public partial class NWDataContext
    {
        partial void InsertCategory(Category instance)
        {
            if(Instance.Date == null)
                Instance.Data = DateTime.Now;
    
            ExecuteDynamicInsert(instance);
        }
    }
    

    EF is a little more complicated see http://msdn.microsoft.com/en-us/library/cc716714.aspx for more info on EF buisiness logic.

提交回复
热议问题