问题描述
我正在尝试将TimeSpan Code First属性映射到SQL服务器。 Code First似乎正在SQL中创建一个Time(7)。但是,.Net中的TimeSpan可以处理比24小时更长的时间,我需要存储超过24小时的事件长度。
I am trying to map a TimeSpan Code First property to SQL server. Code First seems to be creating it as a Time(7) in SQL. However TimeSpan in .Net can handle longer periods than 24 hours and I need to store longer than 24 hour for event length. What is the best way to handle this with Code First.
推荐答案
根据我的如何存储建议在SQL中使用TimeSpan将其存储为秒或票据等。最后,我没有映射TimeSpan列,因为在SQL服务器中没有等效项。我只是创建了一个第二个字段,它将TimeSpan转换成刻度并存储在数据库中。我然后阻止存储TimeSpan
As per my previous question on how to store TimeSpan in SQL I was advised to store it as seconds or tickets etc. In the end I didn't map the TimeSpan column as there is no equivalent in SQL server. I simply created a 2nd field which converted the TimeSpan to ticks and stored that in the DB. I then prevented storing the TimeSpan
public Int64 ValidityPeriodTicks { get; set; }
[NotMapped]
public TimeSpan ValidityPeriod
{
get { return TimeSpan.FromTicks(ValidityPeriodTicks); }
set { ValidityPeriodTicks = value.Ticks; }
}
这篇关于如何将TimeSpan映射到超过24小时的SQL Server代码优先?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!