本文介绍了使用实体框架Codefirst存储TimeSpan-SqlDbType.Time溢出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的数据库中植入一些常量:

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

context.Stages.AddOrUpdate(s => s.Name,
                                   new Stage()
                                   {
                                       Name = "Seven",
                                       Span = new TimeSpan(2, 0, 0),
                                       StageId = 7
                                   });
context.Stages.AddOrUpdate(s => s.Name,
                                   new Stage()
                                   {
                                       Name = "Eight",
                                       Span = new TimeSpan(1, 0, 0, 0),
                                       StageId = 8
                                   });

这在我的EF Codefirst迁移的Seed()函数中。它在第八阶段失败,并显示以下内容:

This is within my Seed() function for EF Codefirst Migrations. It fails at Stage Eight with the following:

为什么我无法存储使用EF的时间跨度?我真的希望我不需要在这里两端进行一些愚蠢的时间到滴答声转换...

Why would I not be able to store a timespan using EF? I really hope I don't need to do some silly time-to-ticks conversion on both ends here...

推荐答案

在这一行:

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

您正在使用此构造函数:

You're using this constructor:

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

因此,您实际上是在创建 TimeSpan 自您将 1 传递给 days 参数以来已超过24小时,而基础数据库类型为时间仅接受00:00-23:59之间的值。

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.

很难告诉您您是否真的想拥有 TimeSpan 1天,或者只是一个错字。

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

如果您真的想要 TimeSpan 超过24小时,我想您必须将字段映射到其他数据库类型(例如 SmallDateTime )。

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),

这篇关于使用实体框架Codefirst存储TimeSpan-SqlDbType.Time溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 01:06