gpt4 book ai didi

c# - 如何修复 datetime2 数据类型到 datetime 数据类型的转换导致值超出范围

转载 作者:太空宇宙 更新时间:2023-11-03 10:24:44 25 4
gpt4 key购买 nike

我正在尝试从另一个数据库中插入数据,这段代码直到今天才有效。现在不再是了。

代码如下:

var itemMember = Db.Members.FirstOrDefault(p => p.Id == CurrentMember.Id);
var itemSelectedName = Db.Topics.FirstOrDefault(p => p.Id == TopicId);
var itemMap = Db.Reeves.FirstOrDefault(p=> p.Member.Id == CurrentMember.Id);
var itemReeve = Db.Reeves.FirstOrDefault(p => p.Member.Id == CurrentMember.Id);
var item = new Problem
{
Name = input.Name,
Text = input.Info,
Topic = itemSelectedName,
WhoIs = itemMember,
Province = itemMap.Province,
District = itemMap.District,
Town = itemMap.Town,
Reeve=itemReeve,
Created=DateTime.Now,
Solved = false,
Read=false
};

Db.Problems.Add(item);
Db.SaveChanges();

代码中没有任何空值,它会抛出一个错误:

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

我该怎么办?

现在也出现了这个错误:

System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while saving entities that do not expose foreign key properties for their relationships. The EntityEntries property will return null because a single entity cannot be identified as the source of the exception. Handling of exceptions while saving can be made easier by exposing foreign key properties in your entity types.

但正如我所说,它一直工作到今天:/

这是 View 模型:

[Display(Name = "Başlık"), Required, StringLength(50)]
public string Name { get; set; }

[Display(Name = "Bilgi")]
[UIHint("TinyMCE")]
public string Info { get; set; }

[Required(ErrorMessage = "Gerekli Alan")]
[Display(Name = "Konu")]
[UIHint("DropDownList")]
[AdditionalMetadata("DataController", "Problem")]
[AdditionalMetadata("DataAction", "ProblemDrop")]
public int? TopicId { get; set; }

这是模型

public class Problem
{
public int Id { get; set; }
public string Name { get; set; }
public string Text { get; set; }
public short Sira { get; set; }
public DateTime Created { get; set; }
public virtual Topic Topic { get; set; }
public virtual Member WhoIs { get; set; }
public virtual Province Province { get; set; }
public virtual Town Town { get; set; }
public virtual District District { get; set; }
public virtual Reeve Reeve { get; set; }
public Boolean Solved { get; set; }
public DateTime SolvedDate { get; set; }
public virtual Member WhoSolved { get; set; }
public Boolean Read { get; set; }
}

已解决:日期为空可能是问题所在吗?

最佳答案

您没有为 SolvedDate 设置值,因此默认值 01\01\01 将被发送到数据库,这会导致错误。

如果您希望 SolvedDate 具有 null 值,您应该将其定义为 Nullable date(DateTime?) 因为 DateTime 是一个 struct 并且不能设置为 null

关于c# - 如何修复 datetime2 数据类型到 datetime 数据类型的转换导致值超出范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32023829/

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