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

标签 c# asp.net-mvc-4 datetime

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

代码如下:

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/

相关文章:

c# - 垂直旋转的文本 block 占据整个宽度,就好像它是水平的一样

rss - 在 MVC4/WebAPI 中创建 RSS 提要

c# - 当我有 2 个具有相同名称但协议(protocol)不同的方法时如何使用操作过滤器

c# - 将 EF 代理对象转换为原始 POCO 对象的最佳方法是什么?

c# - DateTime空值

c# - C# DateTime 中的负值

c# - 属性与方法

c# - .NET 标准库 - 无法加载文件或程序集 'System.Runtime.Caching'

javascript - 八月错误的 Date.parse javascript

c# - 作为 session 从 gridview 传递数据