使用EF4转换为datetime时发生溢出 [英] An overflow occurred while converting to datetime using EF4

查看:111
本文介绍了使用EF4转换为datetime时发生溢出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows应用程序使用SQL压缩4.0数据库,使用EF 4.1和代码优先的方法。
我无法将对象保存到数据库,因为我收到异常,内部异常
转换为datetime时出现溢出
尝试保存类型时引用:

  public class Quotation 
{
public int ID {get;组; }

public string Name {get;组; }

public DateTime DateCreated {get;组; }

public ContactPerson ContactPersonAssigned {get;组; }

public string OurReference {get;组; }

public string QuotationDataString {get;组;我发现这个错误可能是由于我的应用程序设置之间的不匹配造成的。以及关于转换日期的sql compact数据库设置。
我不确定,因为我的sdf数据库文件有一个字段正确名为DateCreated,不可空,类型为datetime。



我刚刚进入SQL压缩。你可以帮我调试这个问题吗?

解决方案

如果您的模型具有DateTime类型的不可空属性,对于该属性的空值,它将自动设置为DateTime.MinValue,它在.net 01/01/0001( MSDN上的DateTime.MinValue



(作为附注,您可以通过实施来更改此行为您自己的 IModelBinder for DateTime,如果尝试的值为空/空且属性不可为空,则可能会抛出验证异常)。



如果您尝试将该值( DateTime.MinValue )保存到数据库中,则如果数据库字段为SQL类型datetime,则会得到转换错误,因为.net DateTime .MinValue小于SQL datetime minvalue(01/01/1753),因此无法转换为sql值。 ( MSDN上的SQL datetime最小值



此更新版本的MS SQL Server不会发生此错误,这些版本的datetime2数据类型允许从01/01/0001到31/12/9999(< a href =http://msdn.microsoft.com/en-us/library/bb677335.aspx =noreferrer> MSDN上的SQL datetime2 )(如果datetime2用于该字段,当然)。


I have a windows application working with a SQL Compact 4.0 database, using EF 4.1 and code-first approach. I cannot save an object to the database since I'm getting an exception, with inner exception "An overflow occurred while converting to datetime" when trying to save the type Quotation:

public class Quotation
{
    public int ID { get; set; }

    public string Name { get; set; }

    public DateTime DateCreated { get; set; }

    public ContactPerson ContactPersonAssigned { get; set; }

    public string OurReference { get; set; }

    public string QuotationDataString { get; set; }
}

I read that this error can be caused by a mismatch between my application settings and the sql compact database settings regarding the conversion of a date. I'm not sure about it, since my sdf database file has a field which is correctly named "DateCreated", not-nullable and of type "datetime".

I'm new to SQL compact. Could you help me debug this problem?

解决方案

If your model has non-nullable property of type DateTime, when you post a form with empty value for that property, it is automatically set to DateTime.MinValue, which is in .net 01/01/0001 (DateTime.MinValue on MSDN)

(As a side note, you can change this behavior by implementing your own IModelBinder for DateTime which could i.e. throw a validation exception if attempted value is null/empty and property is not nullable).

If you try to save that value (DateTime.MinValue) into database, you will get conversion error if database field is of sql type datetime, because .net DateTime.MinValue is less than SQL datetime minvalue (01/01/1753) and therefore cannot be converted to sql value. (SQL datetime min value on MSDN)

This error will not occur on newer versions of MS SQL Server, which have datetime2 datatype which allows values from 01/01/0001 to 31/12/9999 (SQL datetime2 on MSDN) (if datetime2 is used for that field, of course).

这篇关于使用EF4转换为datetime时发生溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆