实体框架时间跨度 - 时间错误(Entity Framework TimeSpan - Time E

2019-07-22 14:59发布

我使用EF 5和C#。

在我的主要项目中,我使用CodeFirst的创建数据库。 在那里,我得到这个实体:

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

入库时间属性是随着时间(7),没有在数据库中创建空。

在我MainProject寄托都工作正确。

但访问从Windows服务项目中的数据库时,(我用的是相同的上下文和模型从MainProject)的在同一个解决方案,我收到此错误:

这条线(在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'.

那是什么问题的原因是什么?

//编辑

有趣的是,我们的主要项目中创建时间列没有我们的直升机。

我换成与我们mainproject的配置服务的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="log\Error.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>

Answer 1:

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

实体框架5 支持TimeSpan 。 它使用time作为SQL背衬的类型。 为支持time型始于2008年的SqlServer有没有可能是你从一个SQLServer 2005实例切换到08或更高版本? 这将导致你所经历的一切突然工作。

值得一提的是,使用与时间跨度EF可能是有问题的作为背衬类型time(7)将只保存一个时间跨度是<长度24小时。 因此,通常使用由floAr提到的解决方法。



Answer 2:

实体框架版本5未映射的时间跨度。 但是,您可以同时使用一种变通方法仍要使用它。

只需将存储时间跨度时间跨度为操作和作为一个Int64映射

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


文章来源: Entity Framework TimeSpan - Time Error