How to fix the datetime2 out-of-range conversion error using DbContext and SetInitializer?

前端 未结 15 820
-上瘾入骨i
-上瘾入骨i 2020-12-07 19:39

I\'m using the DbContext and Code First APIs introduced with Entity Framework 4.1.

The data model uses basic data types such as string

相关标签:
15条回答
  • 2020-12-07 19:57

    Based on user @andygjp's answer, its better if you override the base Db.SaveChanges() method and add a function to override any date which does not fall between SqlDateTime.MinValue and SqlDateTime.MaxValue.

    Here is the sample code

    public class MyDb : DbContext
    {
        public override int SaveChanges()
        {
            UpdateDates();
            return base.SaveChanges();
        }
    
        private void UpdateDates()
        {
            foreach (var change in ChangeTracker.Entries().Where(x => (x.State == EntityState.Added || x.State == EntityState.Modified)))
            {
                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;
                        }
                    }
                }
            }
        }
    }
    

    Taken from the user @sky-dev's comment on https://stackoverflow.com/a/11297294/9158120

    0 讨论(0)
  • 2020-12-07 20:02

    You have to ensure that Start is greater than or equal to SqlDateTime.MinValue (January 1, 1753) - by default Start equals DateTime.MinValue (January 1, 0001).

    0 讨论(0)
  • 2020-12-07 20:05

    You can make the field nullable, if that suits your specific modeling concerns. A null date won't be coerced to a date that isn't within the range of the SQL DateTime type the way a default value would. Another option is to explicitly map to a different type, perhaps with,

    .HasColumnType("datetime2")
    
    0 讨论(0)
  • 2020-12-07 20:06

    I had the same issue and in my case I was setting the date to new DateTime() instead of DateTime.Now

    0 讨论(0)
  • 2020-12-07 20:09

    In my case, after some refactoring in EF6, my tests were failing with the same error message as the original poster but my solution had nothing to do with the DateTime fields.

    I was just missing a required field when creating the entity. Once I added the missing field, the error went away. My entity does have two DateTime? fields but they weren't the problem.

    0 讨论(0)
  • 2020-12-07 20:10

    I'm using Database First and when this error happened to me my solution was to force ProviderManifestToken="2005" in edmx file (making the models compatible with SQL Server 2005). Don't know if something similar is possible for Code First.

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