未在SQL Server数据库中插入值

8
我有一个非常有趣的案例:在我的购物过程中,有两个视图需要显示订单的总价和运费,这个部分视图是通过ShowCartDiscountsPrices操作呈现的。最后,我需要使用Entity Framework将MinDeliveryPriceMinDeliveryPriceDDSDeliveryPriceDeliveryPriceDDS插入到数据库中。
奇怪的问题是,在上周的两种情况下,我的SQL Server数据库中仅有DeliveryPrice的值,而其他三列在我的SQL Server数据库中却是null。当我查看问题并以相同的文章下单一个小时后再次查看时,所有四列都被填充了,在此期间没有人对数据库进行任何更改。
我想知道问题可能是什么,我能否编写一些脚本来记录是否出现了某种错误,无论是在SQL Server还是IIS中?
public ActionResult ShowCartDiscountsPrices(OrderDiscountPriceModel model, bool? ischange, int? deliverypreference)
{
    var cart = ShoppingCart.GetCart(WebsiteContext.CurrentUser != null ? (int?)WebsiteContext.CurrentUser.UserID : null);  

    decimal mindeliveryprice = 0;
    decimal mindeliverypricedds = 0;

    if (deliverypreference != null || Session["DeliveryPreference"] != null)
    {
        var parsedelprice = deliverypreference.HasValue ? deliverypreference.Value == 1 ? Decimal.TryParse(Settings.GetSettingByName("Eshop_SpeedyMinDeliveryPrice"), out mindeliveryprice) :Decimal.TryParse(Settings.GetSettingByName("Eshop_HomeMinDeliveryPrice"), out mindeliveryprice) : Decimal.TryParse(Session["MinDeliveryPrice"].ToString(), out mindeliveryprice );

        var parsedelpricedds = deliverypreference.HasValue ? deliverypreference.Value == 1 ? Decimal.TryParse(Settings.GetSettingByName("Eshop_SpeedyMinDeliveryPriceDDS"), out mindeliverypricedds) : Decimal.TryParse(Settings.GetSettingByName("Eshop_HomeMinDeliveryPriceDDS"), out mindeliverypricedds) : Decimal.TryParse(Session["MinDeliveryPriceDDS"].ToString(), out mindeliverypricedds );

        decimal deliveryprice;
        decimal deliverypricedds;

        ShoppingCartHelper.CalculateArticlesDeliveryPrice(mindeliveryprice, mindeliverypricedds, cart.ComputeTotalTransport(), cart.ComputeTotalTransportDDS(), out deliveryprice, out deliverypricedds);

        model.DeliveryPrice = deliveryprice;
        model.DeliveryPriceDDS = deliverypricedds;
        model.DeliveryPreference = deliverypreference.HasValue ? deliverypreference.Value : (int)Session["DeliveryPreference"];
        model.MinDeliveryPrice = mindeliveryprice;
        model.MinDeliveryPriceDDS = mindeliverypricedds;
        model.FinalSum += deliverypricedds;
    }
    else
    {
        if (isFromLastStep != null && bool.Parse(isFromLastStep.ToString()) == true)
            ViewBag.IncludeDelivery = true;
    }            

    if (deliverypreference != null)
    {
            Session["DeliveryPreference"] = deliverypreference;
            Session["MinDeliveryPrice"] = mindeliveryprice;
            Session["MinDeliveryPriceDDS"] = mindeliverypricedds;
    }

    ViewData.TemplateInfo.HtmlFieldPrefix = "OrderDiscoutPrice";
    return PartialView("_CartDiscountsPrices", model);
}

以下是我的辅助函数,它们仅遍历包含文章的集合并计算总交付价格。

 public decimal ComputeTotalTransport()
        {
            decimal totalarticletransport = 0;                
            decimal articletransport;
            foreach (var article in lineCollection)
            {
                if (decimal.TryParse(article.Article.Transport.ToString(), out articletransport))
                {
                    totalarticletransport += article.Article.Quantity * articletransport;
                    articletransport = 0;
                }
            }
            return totalarticletransport;
        }
public decimal ComputeTotalTransportDDS()
        {
            decimal totaltransportdds = 0;           
            decimal articletransportdds;
            foreach (var article in lineCollection)
            {
                if (decimal.TryParse(article.Article.TransportDDS.ToString(), out articletransportdds))
                {
                    totaltransportdds += article.Article.Quantity * articletransportdds;
                    articletransportdds = 0;
                }
            }
            return totaltransportdds;
        }

3
好的,你的例子需要一些修改。你展示了两个“计算...”函数,但这些似乎没有从你展示的动作代码中调用。你还没有展示任何实例化“model”的代码,也没有在任何地方提交对你的数据库所做的更改。 - Paddy
同意 - 如果可以的话,请展示以下代码:(a) 推送到数据库的模型和 (b) 实际插入数据库的代码。我想知道这是否与模型结构和 ORM 相关。 - scgough
2个回答

7

我建议你进一步扩展你的if表达式,如下所示检查会话值是否大于0

if (Convert.ToString(deliverypreference) != "" && Convert.ToString(Session["DeliveryPreference"]) != "" && Convert.ToDecimal(deliverypreference) > 0 && Convert.ToDecimal(Session["DeliveryPreference"]) > 0)
{ 
    //Your insert logic as you have written.
}

你可能知道,有时候你的会话变量可能不是空字符串或null,但是会话中的值等于0。在这种情况下,你的代码将执行,但数据将无法正确插入。

我想建议的第二个解决方法是通过修改设计模式下的表来禁用SQL Server列中的允许空值选项。


2

你好,我承诺会在我的回答中添加代码,但实际上问题是IIS服务器的重启。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接