Storing TimeSpan with Entity Framework Codefirst - SqlDbType.Time overflow

.net c# ef-code-first entity-framework

Question

I'm attempting to seed my database with some constants:

context.Stages.AddOrUpdate(s => s.Name,
                                   new Stage()
                                   {
                                       Name = "Seven",
                                       Span = new TimeSpan(2, 0, 0),
                                       StageId = 7
                                   });
context.Stages.AddOrUpdate(s => s.Name,
                                   new Stage()
                                   {
                                       Name = "Eight",
                                       Span = new TimeSpan(1, 0, 0, 0),
                                       StageId = 8
                                   });

In my Seed() function for EF Codefirst Migrations, this is contained. Stage Eight is where it fails due to the following:

System.Data.UpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.OverflowException: SqlDbType.Time overflow. Value '1.00:00:00' is out of range. Must be between 00:00:00.0000000 and 23:59:59.9999999.

Why wouldn't EF allow me to store a timespan? I sincerely hope that I won't need to perform any ridiculous time-to-ticks conversions on either end of this.

1
26
6/16/2013 3:03:24 AM

Accepted Answer

This sentence:

Span = new TimeSpan(1, 0, 0, 0)

Use of this constructor by you

public TimeSpan(int days, int hours, int minutes, int seconds);

In other words, you're developing aTimeSpan more than 24 hours have passed since your passing1 to thedays however your underlying Database type is parameterTime which only allows inputs in the range 00:00-23:59.

It's difficult to know if you intended to have aTimeSpan or it's just a misprint, with 1 day.

If you truly desire aTimeSpan if your field's value is more than 24 hours, I assume you'll need to map it to another Database type (suchSmallDateTime ).

To fix a simple typo, simply replace your line to:

Span = new TimeSpan(1, 0, 0),
7
6/16/2013 7:51:20 AM

Popular Answer

    [Browsable(false)]
    [EditorBrowsable(EditorBrowsableState.Never)]
    [Obsolete("Property '" + nameof(Duration) + "' should be used instead.")]        
    public long DurationTicks { get; set; }

    [NotMapped]
    public TimeSpan Duration
    {
#pragma warning disable 618
      get { return new TimeSpan(DurationTicks); }
      set { DurationTicks = value.Ticks; }
#pragma warning restore 618
    }

Update

Since the release of EF Core 2.1, this is now possible using Currency Conversion.

builder.Entity<Stage>()
    .Property(s => s.Span)
    .HasConversion(new TimeSpanToTicksConverter()); // or TimeSpanToStringConverter


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow