问题描述
一个DATETIME2数据类型到datetime数据类型的转换导致超出范围的值。
我的应用程序最近开始表现出这种错误,这是很奇怪的,因为它前面的工作。我没有改变我的字模型的日期时间有关的任何事情。它开始发生时,我增加了新的模式,以我的项目。
My application recently started to show this error, it is quite strange, because it worked earlier. I didn't change anything connected with DateTime in my "Word" Model. It started to happen when I added new Models to my project.
服务器错误显示出来,当我尝试编辑数据。创建和删除工作正常。
Server Error shows up when I try to Edit data. Creating and Deleting works fine.
控制器:
[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?
What's wrong? I don't create any DateTime2 variables, so why my app tries to convert DateTime2 to DateTime?
我用ASP.net MVC使用的EntityFramework。
I use ASP.net MVC with EntityFramework.
推荐答案
DateTime对象默认为DateTime.MinValue时,它的价值没有明确设置。
The DateTime object defaults to DateTime.MinValue when it's value isn't explicitly set.
所以,你必须在你的模型一个DateTime对象没有被置位,并默认为这一点,这是上面提到的,是出于一个DateTime DBTYPE的范围,所以的EntityFramework将其转换为DATETIME2 DBTYPE,然后导致投误差在数据库中。
So you have a DateTime object in your model that isn't being set and is defaulting to this, which as noted above, is out of the range of a DateTime dbtype, so EntityFramework converts it to a DateTime2 dbtype, which then causes the cast error in your database.
要解决这个问题,在模型中检查出所有的datetime对象,并确保它们设置比DateTime.MinValue其他任何东西。如果你不想将值设置到任何东西,然后让两个你的DB和模型那场可空,事情会工作
To solve it, check out all your DateTime objects in your model, and make sure they are set anything other than DateTime.MinValue. If you don't want to set the value to anything, then make that field nullable in both your db and your model, and things will work
NB,对数据库列设置默认不解决这个问题,转换发生的太早,你必须明确地设置值
NB, setting a default on the db column doesn't solve this, the conversion happens too early, you must explicitly set the value
这篇关于一个DATETIME2数据类型到datetime数据类型的转换导致超出范围的值 - 没有使用DATETIME2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!