gpt4 book ai didi

c# - 将 datetime2 数据类型转换为 datetime 数据类型错误

转载 作者:可可西里 更新时间:2023-11-01 08:31:22 25 4
gpt4 key购买 nike

我有一个 Controller :

[HttpPost]
public ActionResult Create(Auction auction)
{
var db = new EbuyDataContext();
db.Auctions.Add(auction);
db.SaveChanges();
return View(auction);
}

模型:

public class Auction
{
public long Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public decimal StartPrice { get; set; }
public decimal CurrentPrice { get; set; }
public DateTime StartTime { get; set; }
public DateTime EndTime { get; set; }}
}

还有一个 View :

@model Ebuy.Website.Models.Auction
@using (Html.BeginForm())
{
<p>
//All the information fields...
@Html.LabelFor(model => model.EndTime)
@Html.EditorFor(model => model.EndTime)
</p>
}

当我尝试运行它时,我收到错误:

The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.

model-controller-view 是从一本书中一对一复制过来的。

我需要在 EndTime 字段中输入什么格式才不会出现此错误?

最佳答案

错误是因为您实际上没有正确设置这些值,请确保根据您的应用程序区域设置设置它们。 (例如,dd/mm/yyyy 表示 en-GB,mm/dd/yyyy 表示 en-US)。

看起来您的数据库也已设置为使用 datetime2 列而不是 不是 datetime 列。

您可以:

A) 修改数据库,将类型从datetime2改为datetime

B) 将模型中的类型更改为 datetime2,方法是:

[Column(TypeName = "DateTime2")]
public DateTime StartTime { get; set; }

[Column(TypeName = "DateTime2")]
public DateTime EndTime { get; set; }

关于c# - 将 datetime2 数据类型转换为 datetime 数据类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16298616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com