The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range?

前端 未结 4 562
野趣味
野趣味 2020-12-01 12:11

I am working on application contains a datepicker and if I set the time in that picker to a very old value or far in the future when I try to save this value in the database

4条回答
  •  青春惊慌失措
    2020-12-01 12:35

    Entity Framework will add default date for the field of value {01/01/0001 00:00:00} and this is outside the range of SQL Date Generation. So to make it work, we need to ask EF not to generate a default date, we can make it nullable by doing the following in the Model of that class.

    In this case a default value is generated for the LastLoggedIn field by EntityFramework. If this field in the datebase can take null value, implies we can make it nullable by doing the follwoing in the model

     [Display(Name = "LastLoggedIn")]
            public DateTime? LastLoggedIn { get; set; }
    

提交回复
热议问题