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

后端 未结 23 1282
旧时难觅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

    It looks like you are using entity framework. My solution was to switch all datetime columns to datetime2, and use datetime2 for any new columns, in other words make EF use datetime2 by default. Add this to the OnModelCreating method on your context:

    modelBuilder.Properties<DateTime>().Configure(c => c.HasColumnType("datetime2"));
    

    That will get all the DateTime and DateTime? properties on all the entities in your model.

    0 讨论(0)
  • 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; }
    
    0 讨论(0)
  • 2020-11-28 20:39

    If you ahve access to the DB, you can change the DB column type from datetime to datetime2(7) it will still send a datetime object and it will be saved

    0 讨论(0)
  • 2020-11-28 20:41

    You can also fix this problem by adding to model (Entity Framework version >= 5)

    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public DateTime CreationDate { get; set; }
    
    0 讨论(0)
  • 2020-11-28 20:41

    If you are using Entity Framework version >= 5 then applying the [DatabaseGenerated(DatabaseGeneratedOption.Computed)] annotation to your DateTime properties of your class will allow the database table's trigger to do its job of entering dates for record creation and record updating without causing your Entity Framework code to gag.

    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public DateTime DateCreated { get; set; }
    
    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public DateTime DateUpdated { get; set; }
    

    This is similar to the 6th answer, written by Dongolo Jeno and Edited by Gille Q.

    0 讨论(0)
  • 2020-11-28 20:43

    This is a common error people face when using Entity Framework. This occurs when the entity associated with the table being saved has a mandatory datetime field and you do not set it with some value.

    The default datetime object is created with a value of 01/01/1000 and will be used in place of null. This will be sent to the datetime column which can hold date values from 1753-01-01 00:00:00 onwards, but not before, leading to the out-of-range exception.

    This error can be resolved by either modifying the database field to accept null or by initializing the field with a value.

    0 讨论(0)
提交回复
热议问题