Why is EF DataBase First not use getdate()?

半腔热情 提交于 2019-12-05 10:21:58

No EF will never use your database default. The reason is that your entity has non nullable DateTime property. This property has by default assigned default value in .NET which is 1.1.0001. EF doesn't know if you assigned that value or if it is default value so it always explicitly passes this value to the database. Same happens if you use nullable type but in this case EF will pass explicitly null. In both cases default value from database will not be applied because that value is applied only when the value from application is not explicitly passed in an insert command - EF explicitly passes all values from entity.

You can do it in your Product entity class constructor,

public class Product{
  public Product(){
     CreateDate =DateTime.Now;
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!