entity frameworkTimeSpan – 时间错误

我正在使用EF 5和c#。

在我的主项目中,我使用CodeFirst来创建我的数据库。 我有这个实体:

public class Shift { public string Name { get; set; } public TimeSpan StartTime { get; set; } public TimeSpan EndTime { get; set; } } 

TimeSpan属性创建为time(7),而不是数据库中的null。

在我的MainProject everthing工作正常。

但是当在同一个解决方案中从Windows服务项目(我使用Mainproject中的相同Context和Models)访问数据库时,我收到此错误:

这一行(在Mainproject中多次使用):

 context.Shifts.Load(); 

结果是

 There is no store type corresponding to the conceptual side type 'Edm.Time(Nullable=True,DefaultValue=,Precision=)' of primitive type 'Time'. 

这个问题的原因是什么?

//编辑

有趣的是我们的主要项目创建了没有我们helo的时间列。

我用我们的mainproject中的配置替换了服务的App.Config文件,现在它有效吗?

我仍然想知道为什么坚韧……

     

您的问题可能由您的SqlServer版本解释。

entity framework5 确实支持TimeSpan 。 它使用time作为Sql支持类型。 支持time类型从SqlServer 2008开始。您是否可能从SqlServer 2005实例切换到08或更高版本? 这会导致一切都突然发挥作用。

值得一提的是,将TimeSpan与EF一起使用可能会有问题,因为time(7)的支持类型只能保存长度<24h的Timespan。 因此,通常使用floAr提到的变通方法。

entity framework版本5不映射时间跨度。 但无论如何,你可以使用变通方法来使用它。

只需将Timepan存储为TimeSpan进行操作,将Intpan存储为Int64进行映射

 public Int64 TimeSpanTicks{ get; set; } [NotMapped] public TimeSpan TimeSpanValue { get { return TimeSpan.FromTicks(TimeSpanTicks); } set { TimeSpanTicks= value.Ticks; } }