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

2024-03-26

在我的代码中,我将多个对象添加到存储库中,我尝试在所有循环结束时运行一次存储库 Save() 函数,并在添加每个对象后调用它。但无论哪种方式,当存储库 .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) 在某处初始化 DateTime。

b) 将 sql 切换为使用 DATETIME2 数据类型,该数据类型确实涵盖了 .NET DateTime 值的整个范围。 [仅限 SQL 2008]

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 LINQ to SQL SubmitChanges() 时,什么会导致 SqlDateTime 溢出? 的相关文章

随机推荐