Entity Framework TimeSpan - Time Error

后端 未结 2 491
故里飘歌
故里飘歌 2020-12-18 07:34

I am using EF 5 and c#.

In my Main Project i use CodeFirst to Create my Database. There i got this Entity:

    public class Shift {
    public string         


        
相关标签:
2条回答
  • 2020-12-18 07:53

    Your issue might be explained by your SqlServer version.

    Entity Framework 5 does support TimeSpan. It uses time as the Sql backing type. Support for the time type began with SqlServer 2008. Is it possible that you switched from a SqlServer 2005 instance to 08 or newer? That would result in everything suddenly working as you experienced.

    It's worth mentioning that using TimeSpan with EF can be problematic as the backing type of time(7) will only hold a Timespan that is <24h in length. Therefore it is common to use the workaround mentioned by floAr.

    0 讨论(0)
  • 2020-12-18 07:59

    Entity Framework version 5 doesn’t map timespan. But ou can use a workaround to use it anyway.

    Just store the timespan as TimeSpan for manipulation and as an Int64 for mapping

    public Int64 TimeSpanTicks{ get; set; }     
    [NotMapped]
    public TimeSpan TimeSpanValue
    {
        get { return TimeSpan.FromTicks(TimeSpanTicks); }
        set { TimeSpanTicks= value.Ticks; }
    }
    
    0 讨论(0)
提交回复
热议问题