实体框架时间跨度-时间错误



我用的是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中一切正常。

但是当从Windows服务项目访问数据库时(我使用相同的上下文&在相同的解决方案中,我收到这个错误:

这行(在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。

我用主项目的配置文件替换了服务的App.Config文件,现在它可以工作了吗?

我仍然想知道为什么强硬…

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net" />
  </configSections>
  <connectionStrings>
    <add name="DevConnectionString" connectionString="Data Source=xxxxxxIntegrated Security=True" providerName="System.Data.SqlClient" />
    <add name="Properties.Settings.DevConnectionString" connectionString="Integrated Security=TrueMultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
  </entityFramework>
  <log4net>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="LogFileAppender" />
    </root>
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="logError.log" />
      <param name="AppendToFile" value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%n%-5p %d{yyyy-MM-dd hh:mm:ss} – %m%n" />
      </layout>
    </appender>
  </log4net>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Windows.Interactivity" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="4.5.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

实体框架版本5不映射时间跨度。但无论如何,你可以使用一个变通方法来使用它。

将时间跨度存储为timespan用于操作,将时间跨度存储为Int64用于映射

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

您的问题可能与您的SqlServer版本有关。

Entity Framework 5 does support TimeSpan。它使用time作为Sql后台类型。对time类型的支持始于SqlServer 2008。是否有可能从SqlServer 2005实例切换到08或更新版本?这将导致一切突然像你所经历的那样工作。

值得一提的是,将TimeSpan与EF一起使用可能会有问题,因为time(7)的后备类型只会保存长度为24小时的TimeSpan。因此,通常使用floAr提到的解决方法。