protected void ButtonSave_Click(object sender, EventArgs e) {
using (IntranETEntities context = new IntranETEntities())
{
CUSTOMER customer = context.CUSTOMERs.FirstOrDefault<CUSTOMER>(p => p.CODE == TxtName.Text);
CUSTOMER_EXTENSION customerextension = null;
IE_CUSTOMER_INFO customerInfo = null;
ACADEMY academy = new ACADEMY();
JOB job = new JOB();
if (customer == null)
{
customer = new CUSTOMER();
context.AddToCUSTOMERs(customer);
customerextension = new CUSTOMER_EXTENSION();
customer.CUSTOMER_EXTENSION.Add(customerextension);
customerInfo = new IE_CUSTOMER_INFO();
customer.IE_CUSTOMER_INFO.Add(customerInfo);
}
else
{
customer.CUSTOMER_EXTENSION.Load();
customer.IE_CUSTOMER_INFO.Load();
customerextension = customer.CUSTOMER_EXTENSION.FirstOrDefault<CUSTOMER_EXTENSION>();
customerInfo = customer.IE_CUSTOMERINFO.First<IE_CUSTOMER_INFO>();
if (customerextension == null)
{
customerextension = new CUSTOMER_EXTENSION();
customer.CUSTOMER_EXTENSION.Add(customerextension);
}
else
customerextension.UPDATESEQ++;
if (customerInfo == null)
{
customer;Info = new IE_CUSTOMER_INFO();
customer.IE_CUSTOMER_INFO.Add(customerInfo);
}
....
customerextension.CREATE_DATE = DateTime.Now;
....
context.SaveChanges();//Error is occuring here.
} }
我正在使用我更新的EntityFramework。当我调试此代码时,我收到一条错误,指出“将datetime2数据类型转换为smalldatetime数据类型会导致超出范围的值。\ r \ n语句已终止。” 我无权更改数据库中列的类型。该列不为空。我该如何解决?
感谢您的回复!!!
答案 0 :(得分:0)
smalldatetime介于1900-01-10和2079-06-06之间。如果您的日期超出此范围(.Net中的日期时间的默认值为0001-01-01),则会出现此错误。
答案 1 :(得分:0)
您将异常标记为在分配时发生:
....
customerextension.CREATE_DATE = DateTime.Now; //Error is occuring here.
....
但我希望在SaveChanges()
通话期间实际发生此异常?
我怀疑在保存对象图中的某个地方还有另一个DateTime
属性(除了你指出的CREATE_DATE之外),而其他属性没有被初始化。 DateTime属性的默认值超出了smalldatetime
所需的范围,因此如果使用默认的DateTime属性创建新的EF对象,它将抛出此异常。