保存到数据库时如何避免空日期转换为 DateTime.Min 值?实体框架

发布于 2025-01-05 17:10:35 字数 1648 浏览 3 评论 0原文

我有一个带有文本框的表单,用于输入日期。如果我提供日期一切都很好。如果我不提供日期(我将其留空),则会发送 DateTime.min (1/1/0001) 值。这会导致错误:将 datetime2 数据类型转换为 datetime 数据类型导致值超出范围

我的日期属性定义为可为空:

public Nullable<System.DateTime> InstallDate { get; set; }

我想要的是写入空值如果我不提供日期,则到数据库。 我知道我可以在更新方法中检查 1/1/0001 并发送 null,但是,我必须这样做吗? 有办法解决这个问题吗?谢谢

编辑:

我解决了问题,将下面的代码添加到我的更新方法中:

tank.InstallDate = tank.InstallDate == DateTime.MinValue ? null : tank.InstallDate;

所以我的代码看起来像这样:

public void UpdateTank(Tank tank)
{
    using (RetailFuelEntities ctx = new RetailFuelEntities())
    {
        tank.InstallDate = tank.InstallDate == DateTime.MinValue ? null : tank.InstallDate;
        ctx.Tanks.Attach(tank);
        ctx.Entry(tank).State = EntityState.Modified;
        ctx.SaveChanges();
    }
}

这是我的ObjectDataSource:

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetTank"
                            TypeName="DataAccess.Fuel.EF4.Tanks" DataObjectTypeName="DataAccess.Fuel.EF4.Tank"
                            DeleteMethod="DeleteTank" InsertMethod="InsertTank" UpdateMethod="UpdateTank">
                            <SelectParameters>
                                <asp:ControlParameter ControlID="TextBoxTankId" Name="TankId" PropertyName="Text"
                                    Type="String" />
                            </SelectParameters>
                        </asp:ObjectDataSource>

我想知道是否可以避免空检查。

I have a form with text box for date input. If I Provide date everything is fine. If I don't provide date (I leave it empty), DateTime.min (1/1/0001) value is sent. And this causes error: The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value

My date property is defined as nullable:

public Nullable<System.DateTime> InstallDate { get; set; }

What I want is null value to be written to database if I don't provide date.
I know I could in my update method check for 1/1/0001 and send null, but, do I have to do that? Is there a way around this problem? Thanks

EDIT:

I solved the problem adding code below into my update method:

tank.InstallDate = tank.InstallDate == DateTime.MinValue ? null : tank.InstallDate;

So my code looks like this:

public void UpdateTank(Tank tank)
{
    using (RetailFuelEntities ctx = new RetailFuelEntities())
    {
        tank.InstallDate = tank.InstallDate == DateTime.MinValue ? null : tank.InstallDate;
        ctx.Tanks.Attach(tank);
        ctx.Entry(tank).State = EntityState.Modified;
        ctx.SaveChanges();
    }
}

Here is my ObjectDataSource:

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetTank"
                            TypeName="DataAccess.Fuel.EF4.Tanks" DataObjectTypeName="DataAccess.Fuel.EF4.Tank"
                            DeleteMethod="DeleteTank" InsertMethod="InsertTank" UpdateMethod="UpdateTank">
                            <SelectParameters>
                                <asp:ControlParameter ControlID="TextBoxTankId" Name="TankId" PropertyName="Text"
                                    Type="String" />
                            </SelectParameters>
                        </asp:ObjectDataSource>

I wonder if null checking can be avoided.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〃温暖了心ぐ 2025-01-12 17:10:35

问题是您的 UI 控件 (FormView) 不会将空字符串转换为 null,而是转换为 DateTime.MinValue。您的财产很高兴接受这个值,因为它毕竟是一个有效的日期。

可以在此处找到解决方案。基本上,有一个专门针对这种情况的参数属性,称为ConvertEmptyStringToNull。

The problem is that your UI control (FormView) doesn't convert an empty string to null, but instead to DateTime.MinValue. Your property happily accepts this value, because it's a valid date after all.

The solution can be found here. Basically, there's a parameter attribute specifically for this case and it's called ConvertEmptyStringToNull.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文