Asp.net mvc 使用LINQ to SQL SubmitChanges()时,什么会导致SqlDateTime溢出?

Asp.net mvc 使用LINQ to SQL SubmitChanges()时,什么会导致SqlDateTime溢出?,asp.net-mvc,linq-to-sql,Asp.net Mvc,Linq To Sql,在我的代码中,有多个对象被添加到存储库中,我尝试在所有循环结束时运行一次repository Save()函数,并在添加的每个对象之后调用它。但是无论哪种方式,当repository.Save()中的db.SubmitChanges()发生错误时,我仍然会得到SqlDateTime溢出。。。有什么想法吗 SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM. Descripti

在我的代码中,有多个对象被添加到存储库中,我尝试在所有循环结束时运行一次repository Save()函数,并在添加的每个对象之后调用它。但是无论哪种方式,当repository.Save()中的db.SubmitChanges()发生错误时,我仍然会得到SqlDateTime溢出。。。有什么想法吗

 SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlTypes.SqlTypeException: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.

Source Error:

Line 155:        public void Save()
Line 156:        {
Line 157:            db.SubmitChanges();
Line 158:        }
Line 159:


Source File: C:\inetpub\wwwroot\Models\OrderRepository.cs    Line: 157


        foreach (string vol in volumes)
        {
            if (vol != "-1" && vol != "")
            {
                Product prod = orderRepository.getProductByVolumeID(System.Convert.ToInt32(vol));
                ProductVolume pvol = orderRepository.getProductVolumeByID(System.Convert.ToInt32(vol));

                OrderProduct oProd = new OrderProduct();
                oProd.OrderID = getOrder.OrderID;
                oProd.VolumeID = pvol.VolumeID;
                orderRepository.AddOrderProduct(oProd);


            }
        }

        foreach (string feat in features)
        {
            if (feat != "")
            {
                Product prod = orderRepository.getProductByID(System.Convert.ToInt32(feat));

                OrderFeature oFeat = new OrderFeature();
                oFeat.OrderID = getOrder.OrderID;
                oFeat.ProductID = prod.ProductID;
                orderRepository.AddOrderFeature(oFeat);


                featuresTotal += prod.UserIntervalPrice;
            }

        }

        totalTotal = volumeTotal + featuresTotal;
        orderRepository.Save();

基本上,问题在于Sql DATETIME数据类型从1/1/1753开始,而DATETIME.MinValue是1/1/0000(或类似的值)。因此,如果不初始化日期属性,就会出现Sql溢出

修复选项:

a) 在某处初始化日期时间


b) 切换sql以使用DATETIME2数据类型,该数据类型覆盖整个.NET DateTime值范围。[SQL 2008 only]

您可以使用and在()分部方法中验证日期。

啊,是的,我完全忘记了我的表中甚至有一个DateTime字段,这个答案刷新了我的记忆并解释了原因,感谢RichardOD评论的固定答案您能详细说明初始化日期属性位吗?
MyObject.MyDateTime=DateTime.Now
。您知道,您只需将变量设置为sql不会吐出的内容。