c# - 将 datetime2 数据类型转换为 datetime 数据类型导致值超出范围 - 未使用 DateTime2

标签 c# asp.net-mvc entity-framework

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

我的应用程序最近开始显示此错误,这很奇怪,因为它以前工作过。在我的“Word”模型中,我没有更改与 DateTime 相关的任何内容。当我将新模型添加到我的项目时,它就开始发生了。

当我尝试编辑数据时出现服务器错误。创建和删除工作正常。

Controller :

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "ID,UsersLanguage,OtherLanguage,Notes")] Word word, int idOfCollection)
    {
        if (ModelState.IsValid)
        {
            db.Entry(word).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index", new { idOfCollection = idOfCollection });
        }
        return View(word);
    }

型号:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace WebLanguageTeacher.Models.MyDatabase
{
    public class Word
    {
        public int ID { get; set; }
        [MinLength(2, ErrorMessage = "Wydaje mi się, że słowo powinno mieć przynajmniej 2 litery ;)")] 
        [DisplayName("Język Użytkownika")]
        [Required]
        public string UsersLanguage { get; set; }
        [MinLength(2, ErrorMessage = "Wydaje mi się, że słowo powinno mieć przynajmniej 2 litery ;)")] 
        [DisplayName("Inny język")]
        [Required]
        public string OtherLanguage { get; set; }
        [DisplayName("Notatki")]
        public string Notes { get; set; }
        [DisplayName("Ostatnia powtórka")]
        public DateTime LastReviewed { get; set; }
        [DisplayName("Następna powtórka")]
        public DateTime NextReview { get; set; }
        [DefaultValue(0)]
        [DisplayName("Przerwa między powtórkami")]
        public int ReviewInterval { get; set; } /*W miejsce Difficulty*/

        [DisplayName("Nazwa właściciela")]
        public string OwnerName { get; set; }
        public virtual Collection Collection { get; set; }

        [NotMapped]
        public bool ModifyReview { get; set; } /* Klient przesyła tylko za ile dni będzie następna powtórka, serwer sam generuje datę*/

        public Word(){
            ModifyReview = false;
        }
    }
}

怎么了?我没有创建任何 DateTime2 变量,那么为什么我的应用会尝试将 DateTime2 转换为 DateTime?

我将 ASP.net MVC 与 EntityFramework 结合使用。

最佳答案

如果未明确设置 DateTime 对象的值,则默认为 DateTime.MinValue。

所以你的模型中有一个 DateTime 对象没有被设置并且默认为此,如上所述,它超出了 DateTime dbtype 的范围,所以 EntityFramework 将它转换为 DateTime2 dbtype,然后导致数据库中的转换错误。

要解决此问题,请检查模型中的所有 DateTime 对象,并确保它们的设置不是 DateTime.MinValue。如果您不想将该值设置为任何值,那么在您的数据库和模型中将该字段设置为可为空,一切都会起作用

注意,在 db 列上设置默认值并不能解决这个问题,转换发生得太早,您必须显式设置值

关于c# - 将 datetime2 数据类型转换为 datetime 数据类型导致值超出范围 - 未使用 DateTime2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28062192/

相关文章:

c# - 如何检索 Entity Framework 中的特定列

c# - MVC ViewModel 未在 View 中填充

asp.net-mvc - 如何将 Html.ValidationSummary 与 Ajax.BeginForm 一起使用?

c# - 使用外键链接的集合保存实体时出现 EF 错误

.net - 如何将 ObjectStateEntry.OriginalValues 转换为实体?

c# - ASP .NET C# - 将圆形 SqlDataSource 放入方形 DataTable 孔中?

c# - 使用 async/await 对大型 C# 应用程序进行多线程处理

C# 泛型转换

c# - 避免在 ASP.NET MVC 5 应用程序的 Action 上使用 OnResultExecuted Filter

c# - Entity Framework 4.0 : Method cannot be translated into a store expression