代码优先实体框架中的datetime2的ASP.Net问题

时间:2016-10-22 11:03:28

标签: asp.net entity-framework ef-migrations datetime2

我有Game类,它有两个属性GameStartDateGameEndDate 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);

    }

正如您所看到的,我已尝试在游戏类中使用nullabledatetime2

        [Column(TypeName = "datetime2")]
        public DateTime? GameEndDate { get; set; }

但我仍然收到错误:

  

将datetime2数据类型转换为日期时间数据类型   导致超出范围的值

在程序包管理器控制台中运行update-database命令后。能告诉我为什么会发生这种情况以及如何解决这个问题吗?

由于

0 个答案:

没有答案