C# 实体框架时间跨度-时间错误
我使用的是EF 5和c 在我的主要项目中,我使用CodeFirst创建数据库。我得到了这个实体:C# 实体框架时间跨度-时间错误,c#,entity-framework,service,C#,Entity Framework,Service,我使用的是EF 5和c 在我的主要项目中,我使用CodeFirst创建数据库。我得到了这个实体: public class Shift { public string Name { get; set; } public TimeSpan StartTime { get; set; } public TimeSpan EndTime { get; set; } } TimeSpan属性在数据库中创建为时间(7),不为空 在我的主要项目中,一切正常 但在同一
public class Shift {
public string Name { get; set; }
public TimeSpan StartTime { get; set; }
public TimeSpan EndTime { get; set; }
}
TimeSpan属性在数据库中创建为时间(7),不为空
在我的主要项目中,一切正常
但在同一解决方案中从Windows服务项目(我使用Main项目中的相同上下文和模型)访问数据库时,我收到以下错误:
该行(在主项目中多次使用):
导致
There is no store type corresponding to the conceptual side type 'Edm.Time(Nullable=True,DefaultValue=,Precision=)' of primitive type 'Time'.
这个问题的原因是什么
//编辑
有趣的是,我们的主要项目在没有直升机的情况下创建了时间栏
我用主项目中的配置替换了服务的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>
实体框架版本5没有映射时间跨度。但无论如何,您都可以使用变通方法来使用它 只需将timespan存储为timespan进行操作,并将其存储为Int64进行映射
public Int64 TimeSpanTicks{ get; set; }
[NotMapped]
public TimeSpan TimeSpanValue
{
get { return TimeSpan.FromTicks(TimeSpanTicks); }
set { TimeSpanTicks= value.Ticks; }
}
您的问题可能由SqlServer版本来解释 实体框架5不支持
TimeSpan
。它使用time
作为Sql支持类型。对time
类型的支持始于SQLServer2008。您是否可能从SQLServer2005实例切换到08或更高版本?这会导致所有事情都像你所经历的那样突然运转起来
值得一提的是,将TimeSpan与EF一起使用可能会有问题,因为
time(7)
的备份类型将只保留一个TimeSpan,这也避免了在db中存储值时出现任何问题,并且会损失刻度的精度,因为IIRC如果使用time(7),精度会有所不同出于显而易见的原因,我解决了我的问题,但仍然不知道为什么会解决(请参见编辑)。有什么想法吗?实体框架版本5没有映射时间跨度。我使用的是NuGet的EF5,它只是映射了一个TimeSpan
属性。例如,这种方法的问题是不能在查询中使用unmap属性。为什么使用Int64
而不是int
或long
?我刚刚使用SqlServer Compact Edition(SQLCE)遇到了这个问题。看来这是一个潜在的数据库问题。
public Int64 TimeSpanTicks{ get; set; }
[NotMapped]
public TimeSpan TimeSpanValue
{
get { return TimeSpan.FromTicks(TimeSpanTicks); }
set { TimeSpanTicks= value.Ticks; }
}