C# 将datetime2数据类型转换为datetime数据类型导致值超出范围异常
我试图只在数据库表中插入日期和卡片,我得到了以下错误 错误异常{“将datetime2数据类型转换为datetime数据类型导致值超出范围。\r\n语句已终止。“} 这是我的桌子结构 这是我的课时C# 将datetime2数据类型转换为datetime数据类型导致值超出范围异常,c#,sql,entity-framework,C#,Sql,Entity Framework,我试图只在数据库表中插入日期和卡片,我得到了以下错误 错误异常{“将datetime2数据类型转换为datetime数据类型导致值超出范围。\r\n语句已终止。“} 这是我的桌子结构 这是我的课时 public class TimeoutJustification { public long ID { get; set; } public DateTime Date { get; set; } public string Card
public class TimeoutJustification
{
public long ID { get; set; }
public DateTime Date { get; set; }
public string Card { get; set; }
public DateTime TimeIn { get; set; }
public DateTime TimeOut { get; set; }
public long TotalTime { get; set; }
[NotMapped]
public TimeSpan TotalTimeSpan
{
get
{
return new TimeSpan(TotalTime);
}
set
{
TotalTime = value.Ticks;
}
}
public string Justification { get; set; }
public string Details { get; set; }
public string GeneralJustification { get; set; }
}
}
将datetime2数据类型转换为datetime数据类型时出现错误异常,导致值超出范围。\r\n语句已终止。“在保存到数据库时捕获。”
因此,我使用实体框架,数据库中的ID是自动递增的
下面是一个它看起来像什么的例子
List<TimeoutJustification>withOutTimeOuts;
withOutTimeOuts.Add(new TimeOutJustification{Card="1234",Date="12-03-19"}
db.JustificationTable.AddRange(withOutTimeOuts);
//Error Gets Caught here
db.SaveChanges();
没有超时的列表;
无超时。添加(新的超时调整{Card=“1234”,Date=“12-03-19”}
db.justicatable.AddRange(无超时);
//错误在这里被发现
db.SaveChanges();
datetime 2
比datetime
覆盖的范围更大
日期时间2:
日期范围0001-01-01至9999-12-31
日期时间:
日期范围1753年1月1日至9999年12月31日
Date=“12-03-19”
可能会转换为12年(或19年)。解决方案可能是对Date
属性使用DateTime
:
public class TimeOutJustification
{
public DateTime Date { get; set; }
...
}
请发布
TimeOutJustification
类。不清楚DateTime属性是否可为空。或者。我已经将DateTime用于我的日期属性。这不是问题所在。它位于其他日期属性中。