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

后端 未结 23 1332
旧时难觅i
旧时难觅i 2020-11-28 20:19

I have the following code in my HomeController:

public ActionResult Edit(int id)
{
    var ArticleToEdit = (from m in _db.ArticleSet where m.storyId == id se         


        
23条回答
  •  情深已故
    2020-11-28 20:39

    I had the same problem, unfortunately, I have two DateTime property on my model and one DateTime property is null before I do SaveChanges.

    So make sure your model has DateTime value before saving changes or make it nullable to prevent error:

    public DateTime DateAdded { get; set; }   //This DateTime always has a value before persisting to the database.
    public DateTime ReleaseDate { get; set; }  //I forgot that this property doesn't have to have DateTime, so it will trigger an error
    

    So this solves my problem, its a matter of making sure your model date is correct before persisting to the database:

    public DateTime DateAdded { get; set; }
    public DateTime? ReleaseDate { get; set; }
    

提交回复
热议问题