Database default value is not inserted while create record by entity framework

前端 未结 3 1743
庸人自扰
庸人自扰 2021-02-15 17:33

I have a LoginRecord table in sqlserver 2008 with the following column structure-

LoginId      - int, identity
UserId       - int
LoginDateTime- Allow nulls fals         


        
3条回答
  •  梦如初夏
    2021-02-15 17:55

    Setting default values in Entity Framework 5 and 6 by changing T4 Template File Made below changes in .tt(template file) Example: 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 this https://www.youtube.com/watch?v=i8J2ipImMuU

    Helpfull

提交回复
热议问题