简体   繁体   English

实体框架TimeSpan - 时间错误

[英]Entity Framework TimeSpan - Time Error

I am using EF 5 and c#. 我正在使用EF 5和c#。

In my Main Project i use CodeFirst to Create my Database. 在我的主项目中,我使用CodeFirst来创建我的数据库。 There i got this Entity: 我有这个实体:

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

The TimeSpan Properties are created as time(7), not null in the Database. TimeSpan属性创建为time(7),而不是数据库中的null。

In my MainProject everthing works proper. 在我的MainProject everthing工作正常。

But when accessing the Database from a Windows Service Project ( I use the same Context & Models from the Mainproject) in the same Solution i receive this error: 但是当从同一个解决方案中的Windows服务项目(我使用Mainproject中的相同Context和Models)访问数据库时,我收到此错误:

This line (Multiple times used in the Mainproject): 这一行(在Mainproject中多次使用):

context.Shifts.Load();

results in 结果是

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

What is the cause of that issue? 这个问题的原因是什么?

//edit //编辑

The funny thing is our main project created the time Column without our helo. 有趣的是我们的主要项目创建了没有我们helo的时间列。

i replaced the App.Config file of the Service with the config from our mainproject and now it works? 我用我们的mainproject中的配置替换了服务的App.Config文件,现在它有效吗?

I still wonder why tough... 我仍然想知道为什么坚韧......

<?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>

Entity Framework version 5 doesn't map timespan. 实体框架版本5不映射时间跨度。 But ou can use a workaround to use it anyway. 但无论如何,你可以使用一种解决方法来使用它。

Just store the timespan as TimeSpan for manipulation and as an Int64 for mapping 只需将Timepan存储为TimeSpan进行操作,将Intpan存储为Int64进行映射

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

Your issue might be explained by your SqlServer version. 您的问题可能由您的SqlServer版本解释。

Entity Framework 5 does support TimeSpan . 实体框架5 确实支持TimeSpan It uses time as the Sql backing type. 它使用time作为Sql支持类型。 Support for the time type began with SqlServer 2008. Is it possible that you switched from a SqlServer 2005 instance to 08 or newer? 从SqlServer 2008开始支持time类型。是否有可能从SqlServer 2005实例切换到08或更高版本? 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. 值得一提的是,将TimeSpan与EF一起使用可能会有问题,因为time(7)的支持类型只能保存长度<24h的Timespan。 Therefore it is common to use the workaround mentioned by floAr. 因此,通常使用floAr提到的解决方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM