实体框架时间跨度 - 时间错误 [英] Entity Framework TimeSpan - Time Error

查看:208
本文介绍了实体框架时间跨度 - 时间错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用EF 5和C#。



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

 公共类移{
公共字符串名称{;组; }
公开时间跨度开始时间{搞定;组; }
公开时间跨度结束时间{搞定;组; }
}

的时间跨度属性创建为时间(7),而不是在空数据库。



在我MainProject全部的寄托工作正确的。



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



本线(在Mainproject多次使用):

  context.Shifts.Load(); 



结果

 没有对应于基本类型时间的概念侧式'Edm.Time(可空= TRUE,默认值=,=精)没有店面​​类型。 



那是什么问题?



的原因

//修改



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



我换成与我们mainproject的配置服务的app.config文件,现在它的作品?



我仍然不知道为什么坚韧...

 < ;? XML版本=1.0编码=UTF-8>?; 
<结构>
< configSections>
<! - 有关Entity Framework的配置的详细信息,请访问http://go.microsoft.com/fwlink/?LinkID=237468 - >
<节名称=的EntityFrameworkTYPE =System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,的EntityFramework,版本5.0.0.0 =文化=中性公钥= b77a5c561934e089requirePermission =FALSE/> ;
<节名称=log4net的TYPE =log4net.Config.Log4NetConfigurationSectionHandler,log4net的/>
< / configSections>
<&是connectionStrings GT;
<添加名称=DevConnectionString的connectionString =数据源= xxxxxxIntegrated安全=真的providerName =System.Data.SqlClient的/>
<添加名称=Properties.Settings.DevConnectionString的connectionString =集成安全性= TrueMultipleActiveResultSets = TRUE的providerName =System.Data.SqlClient的/>
< /&是connectionStrings GT;
<&启动GT;
< supportedRuntime版本=V4.0SKU =.net框架,版本= V4.5/>
< /启动>
<&的EntityFramework GT;
< defaultConnectionFactory TYPE =System.Data.Entity.Infrastructure.SqlConnectionFactory,的EntityFramework/>
< /&的EntityFramework GT;
<&log4net的GT;
<根和GT;
<电平值=DEBUG/>
<附加目的地-REF REF =LogFileAppender/>
< /根>
<追加程序名称=LogFileAppenderTYPE =log4net.Appender.RollingFileAppender>
< PARAM NAME =文件VALUE =log\Error.log/>
< PARAM NAME =AppendToFileVALUE =真/>
< rollingStyle值=大小/>
< maxSizeRollBackups值=10/>
< maximumFileSize值=10MB/>
< staticLogFileName值=真/>
<布局类型=log4net.Layout.PatternLayout>
< PARAM NAME =ConversionPatternVALUE =%N%-5p%d个{YYYY-MM-DD HH:MM:SS} - %M%N/>
< /布局>
< /附加器>
< / log4net的>
<&运行GT;
< assemblyBinding的xmlns =瓮:架构 - 微软COM:asm.v1>
< dependentAssembly>
< assemblyIdentity名称=System.Windows.Interactivity公钥=31bf3856ad364e35文化=中性/>
< bindingRedirect oldVersion =0.0.0.0-4.5.0.0NEWVERSION =4.5.0.0/>
< / dependentAssembly>
< / assemblyBinding>
< /运行>
< /结构>


解决方案

您的问题可能会被你的SqlServer版本的解释。



实体框架5的确实的支持时间跨度。它采用时间作为SQL后盾类型。为时间类型开始与2008年SqlServer的支持是有可能,你从一个SQLServer 2005实例切换到08或更高版本?这将导致你所经历的一切突然工作。



这是值得一提的是,使用时间跨度与EF可能会产生问题为后盾类型的时间(在长24小时; 7)将只持有一个时间跨度是为<。因此,通常使用由floAr提到的解决方法。


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

In my MainProject everthing works proper.

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:

This line (Multiple times used in the 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.

i replaced the App.Config file of the Service with the config from our mainproject and now it works?

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>

解决方案

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.

这篇关于实体框架时间跨度 - 时间错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆