我有Game类,它有两个属性GameStartDate
和GameEndDate
DateTime
类型格式。
public class Game
{
[Key]
public int GameID { get; set; }
[Required, DisplayName("Start Date"), DataType(DataType.Date)]
[Column(TypeName = "datetime2")]
public DateTime? GameStartDate { get; set; }
[Required, DisplayName("End Date"), DataType(DataType.Date)]
[Column(TypeName = "datetime2")]
public DateTime? GameEndDate { get; set; }
}
在seed
方法中
protected override void Seed(Models.Game.GameCTX context)
{
context.Games.AddOrUpdate(getEvents(context).ToArray());
context.SaveChanges();
}
private List<Game> getEvents(GameCTX db)
{
List<Game> events = new List<Game>();
events.Add(new Game()
{
StartDate = DateTime.Today,
EndDate = DateTime.Today,
}
);
events.Add(new Game()
{
StartDate = DateTime.Today,
EndDate = DateTime.Today,
}
);
return events;
}
并且在Up
方法中我有
public override void Up()
{
CreateTable(
"dbo.Events",
c => new
{
EventID = c.Int(nullable: false, identity: true),
GameStartDate = c.DateTime(nullable: false, precision: 7, storeType: "datetime2"),
GameEndDate = c.DateTime(nullable: false, precision: 7, storeType: "datetime2"),
})
.PrimaryKey(t => t.GameID);
}
正如您所看到的,我已尝试在游戏类中使用nullable
和datetime2
:
[Column(TypeName = "datetime2")]
public DateTime? GameEndDate { get; set; }
但我仍然收到错误:
将datetime2数据类型转换为日期时间数据类型 导致超出范围的值
在程序包管理器控制台中运行update-database
命令后。能告诉我为什么会发生这种情况以及如何解决这个问题吗?
由于