1
  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语句已终止。” 我无权更改数据库中列的类型。并且该列不为空。我该如何解决?

感谢您的回复!!!

4

2 回答 2

1

您将异常标记为在分配时发生:

 ....
 customerextension.CREATE_DATE = DateTime.Now; //Error is occuring here.
 ....

但我希望这个异常在SaveChanges()通话期间实际发生?

我怀疑DateTime在保存的对象图中某处还有另一个属性(除了您指出的 CREATE_DATE ),并且其他属性没有被初始化。DateTime 属性的默认值超出了 所需的范围smalldatetime,因此如果您创建一个具有默认 DateTime 属性的新 EF 对象,它将引发此异常。

于 2013-06-06T14:02:31.710 回答
0

smalldatetime介于 1900-01-10 和 2079-06-06 之间。如果您的日期超出此范围(.Net 中日期时间的默认值为 0001-01-01),您将收到此错误。

于 2013-06-06T07:42:20.977 回答