Set default value in EF designer datetime

后端 未结 5 1689
终归单人心
终归单人心 2021-01-03 20:43

Trying to set the default value of a datetime field to take current time. In SQL\'s field designer i\'d use getdate(). What should i use in Entity Framework

5条回答
  •  无人及你
    2021-01-03 20:57

    Setting default values in Entity Framework 5 and 6 by changing T4 Template File

    Made below changes in .tt(template file) remove if condition at line 34

    34 if (propertiesWithDefaultValues.Any() || collectionNavigationProperties.Any() || complexProperties.Any())
    35 {
    

    add

    59 OnCreated();
    60 }
    61
    62 partial void OnCreated();
    63 <#
    

    refer this image http://i.stack.imgur.com/DdlNB.png red means remove and green means add

    This will add constructor in all entity classes with OnCreated method.

    Like below

    public partial class Category
    {
        public Category()
        {
          this.Products = new HashSet();
          OnCreated();
        }
    
     partial void OnCreated();
     public int Id { get; set; }
     public string Name { get; set; }
    
     public virtual ICollection Products { get; set; }
    }
    

    Then create class file using same namespace that of Entities.

    public partial class Category
    {
       partial void OnCreated()
       {
          Name = "abc"
       }
    }
    

    refer below answer for more details https://stackoverflow.com/a/38882032/5475124

提交回复
热议问题