Entity Framework TimeSpan - Time Error

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; } } 

TimeSpan properties are created as time (7), not null in the database.

In my MainProject, everything is working correctly.

But when accessing the database from a Windows service project (I use the same context and models from the main project) in the same solution, I get this error:

This line (used several times in the main project):

 context.Shifts.Load(); 

leads to

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

//edit

The funny thing is that our main project created a temporary column without our helo.

i replaced the App.Config Services file with the configuration from our main project and now it works?

I'm still wondering why it's cool ...

 <?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> 
+4
source share
2 answers

Your problem can be explained by your version of SqlServer.

Entity Framework 5 supports TimeSpan . It uses time as a type of Sql support. Support for the time type started with SqlServer 2008. Is it possible that you switched from an instance of SqlServer 2005 to 08 or later? This would make everything suddenly work as you experienced.

It is worth mentioning that using TimeSpan with EF can be problematic, as the support type time(7) will contain only a time interval whose length is <24h. Therefore, the workaround mentioned by floAr is usually used.

+5
source

Entity Framework version 5 does not display time intervals. But ou can use a workaround to use it anyway.

Just save the time interval as TimeSpan for manipulation and as Int64 for display

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

All Articles