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

后端 未结 23 1284
旧时难觅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 21:00

    The model should have nullable datetime. The earlier suggested method of retrieving the object that has to be modified should be used instead of the ApplyPropertyChanges. In my case I had this method to Save my object:

    public ActionResult Save(QCFeedbackViewModel item)
    

    And then in service, I retrieve using:

    RETURNED = item.RETURNED.HasValue ? Convert.ToDateTime(item.RETURNED) : (DateTime?)null 
    

    The full code of service is as below:

     var add = new QC_LOG_FEEDBACK()
                {
    
                    QCLOG_ID = item.QCLOG_ID,
                    PRE_QC_FEEDBACK = item.PRE_QC_FEEDBACK,
                    RETURNED = item.RETURNED.HasValue ? Convert.ToDateTime(item.RETURNED) : (DateTime?)null,
                    PRE_QC_RETURN = item.PRE_QC_RETURN.HasValue ? Convert.ToDateTime(item.PRE_QC_RETURN) : (DateTime?)null,
                    FEEDBACK_APPROVED = item.FEEDBACK_APPROVED,
                    QC_COMMENTS = item.QC_COMMENTS,
                    FEEDBACK = item.FEEDBACK
                };
    
                _context.QC_LOG_FEEDBACK.Add(add);
                _context.SaveChanges();
    
    0 讨论(0)
  • 2020-11-28 21:03

    This one was driving me crazy. I wanted to avoid using a nullable date time (DateTime?). I didn't have the option of using SQL 2008's datetime2 type either (modelBuilder.Entity<MyEntity>().Property(e => e.MyDateColumn).HasColumnType("datetime2");).

    I eventually opted for the following:

    public class MyDb : DbContext
    {
        public override int SaveChanges()
        {
            UpdateDates();
            return base.SaveChanges();
        }
    
        private void UpdateDates()
        {
            foreach (var change in ChangeTracker.Entries<MyEntityBaseClass>())
            {
                var values = change.CurrentValues;
                foreach (var name in values.PropertyNames)
                {
                    var value = values[name];
                    if (value is DateTime)
                    {
                        var date = (DateTime)value;
                        if (date < SqlDateTime.MinValue.Value)
                        {
                            values[name] = SqlDateTime.MinValue.Value;
                        }
                        else if (date > SqlDateTime.MaxValue.Value)
                        {
                            values[name] = SqlDateTime.MaxValue.Value;
                        }
                    }
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-28 21:04

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

    This error occurred when due to NOT assigning any value against a NOT NULL date column in SQL DB using EF and was resolved by assigning the same.

    Hope this helps!

    0 讨论(0)
  • 2020-11-28 21:04

    you have to match the input format of your date field to the required entity format which is yyyy/mm/dd

    0 讨论(0)
  • 2020-11-28 21:05

    If you have a column that is datetime and allows null you will get this error. I recommend setting a value to pass to the object before .SaveChanges();

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