Time Usage with EF 6

Asked

Viewed 114 times

4

I have a class with the following property.

  public TimeSpan TempoIdeal { get; set; }

Estou atribuindo esta propriedade com 

TempoIdeal = new TimeSpan(days: 3, hours: 15, minutes:10, seconds: 0);
 

While trying to enter I get this error Overflow

Sqldbtype.Time. The value '3.03:03:00' is out of range. It must be between 00:00:00.0000000 and 23:59:59.9999999.

In SQL this property is being mapped as Time(7);

I am using EF 6 with ASP.NET 4.52

1 answer

3


Berechit, the type Time in the Sql-Server was not designed to store time intervals, or relative time (as in your example: the product took 3 dias, 15 horas e 10 minutos to be delivered), but to store an exact time (as in your example: the product will be delivered the 15:10).

In this way, it makes no sense to store values higher than 23:59:59.9999999. So if you really need to store values greater than``24 hours, lhe aconselho à utilizar um campoBigintnoSql-Server` and do the following mapping.

public Int64 TempoIdealTicks { get; set; }

[NotMapped]
public TimeSpan TempoIdeal
{
    get { return TimeSpan.FromTicks(this.TempoIdealTicks); }
    set { this.TempoIdealTicks = value.Ticks; }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.