Storing TimeSpan with Entity Framework Codefirst - SqlDbType.Time overflow

后端 未结 4 836
攒了一身酷
攒了一身酷 2021-02-05 02:41

I\'m trying to seed some constants into my DB:

context.Stages.AddOrUpdate(s => s.Name,
                                   new Stage()
                                


        
4条回答
  •  半阙折子戏
    2021-02-05 03:24

    In this line:

    Span = new TimeSpan(1, 0, 0, 0)
    

    You're using this constructor:

    public TimeSpan(int days, int hours, int minutes, int seconds);
    

    So you're actually creating a TimeSpan greater than 24 hours since you're passing 1 to the days parameter, while your underlying Database type is Time which only accepts values between 00:00-23:59.

    Hard to tell whether you actually meant to have a TimeSpan with 1 day, or it's just a typo.

    If you really want a TimeSpan greater than 24 hours, i guess you'll have to map your field to another Database type (like SmallDateTime).

    If it's just a typo error, just change your line to:

    Span = new TimeSpan(1, 0, 0),
    

提交回复
热议问题