在MVC App中批量更新单个数据库

时间:2014-05-19 14:02:30

标签: c# asp.net-mvc linq entity-framework

编辑2:在查看了其他问题和你们这里可爱的人的建议后,我找到了答案,现在这个程序真的很棒!谢谢你们!

编辑:我已根据其他用户的建议更新了我的代码。说真的,你们真是太棒了!

我有一个适用于我的本地主机的页面,但不适用于我的开发/测试服务器。

以下是页面的工作原理:

  1. 用户上传xml文件和帖子。
  2. 程序解析Xml文件以创建BoxViewModel列表。
  3. 程序使用a删除Boxes表中的所有框 function-import名为DeleteAllBoxes。所有这个函数导入都是执行" DELETE FROM dbo.Boxes;"
  4. 使用BoxViewModels列表,程序更新数据库 所有的盒子。
  5. 这在我的本地主机上完美运行(我已经验证所有312个盒子都成功输入到box表中),但是当我在Dev / Test服务器上使用相同的文件进行尝试时,唯一发生的是所有的框被删除。此外,在我的localhost上的程序完成之前大约需要一两分钟,但Dev / Test上的程序只需几秒钟即可完成。我希望Dev / Test至少和我的localhost一样长。我不认为这是文件大小问题,因为文件大小只有112Kb。它就像带有boxList的foreach循环没有任何框,或者被完全忽略。

    这是我的代码以防万一。我认为解决方案可能是我需要在web.config中设置的设置,但我的想法中也可能出错。

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult FileUpload(HttpPostedFileBase fileFromPage)
        {
            if (fileFromPage != null) //User must upload a file.
            {
                if (fileFromPage.ContentType == "text/xml") //File must be an xml file.
                {
                    List<BoxViewModel> boxesList = new List<BoxViewModel>();
    
                    try //No errors will be caught if all boxes in the xml file are valid.
                    {
    
                        XmlDocument xmlDoc = new XmlDocument();
                        xmlDoc.Load(fileFromPage.InputStream);
    
                        XmlNodeList boxNodeList = xmlDoc.SelectNodes("/initialize/boxes/box");
    
                        //Write each box in the xml to a BoxViewModel, then add to the boxesList for updating the database later.
                        foreach (XmlNode boxNode in boxNodeList)
                        {
                            XmlNode currentBoxNode = boxNode;
                            BoxViewModel currentBox = new BoxViewModel();
    
                            currentBox.Compliment = currentBoxNode.SelectSingleNode("compliment").InnerText;
                            currentBox.Description = currentBoxNode.SelectSingleNode("description").InnerText;
                            currentBox.BoxType = currentBoxNode.SelectSingleNode("boxtype").InnerText;
                            currentBox.InsideLength = decimal.Parse(currentBoxNode.SelectSingleNode("il").InnerText);
                            currentBox.InsideBreadth = decimal.Parse(currentBoxNode.SelectSingleNode("ib").InnerText);
                            currentBox.InsideHeight = decimal.Parse(currentBoxNode.SelectSingleNode("ih").InnerText);
                            currentBox.OutsideLength = decimal.Parse(currentBoxNode.SelectSingleNode("ol").InnerText);
                            currentBox.OutsideBreadth = decimal.Parse(currentBoxNode.SelectSingleNode("ob").InnerText);
                            currentBox.OutsideHeight = decimal.Parse(currentBoxNode.SelectSingleNode("oh").InnerText);
                            currentBox.BoxWeight = decimal.Parse(currentBoxNode.SelectSingleNode("bw").InnerText);
                            currentBox.BoxGrossWeight = decimal.Parse(currentBoxNode.SelectSingleNode("bgw").InnerText);
                            currentBox.BoxStrength = int.Parse(currentBoxNode.SelectSingleNode("boxstrength").InnerText);
    
                            if (string.IsNullOrEmpty(currentBox.Compliment))
                                throw new Exception();
                            if (string.IsNullOrEmpty(currentBox.Description))
                                throw new Exception();
                            if (string.IsNullOrEmpty(currentBox.BoxType))
                                throw new Exception();
    
                            boxesList.Add(currentBox);
                        }
                    }
                    catch
                    {
                        ViewBag.MsgText = "There was an error when processing the file!";
                        ViewBag.MsgColor = "Red";
                    }
    
                    try
                    {
                        if (boxesList.Count > 0) //Only update if there is at least one box.
                        {
                            dbEntities.DeleteAllBoxes(); //Clear the Boxes table for the new boxes.
    
                            foreach (BoxViewModel box in boxesList) //For each box in the boxesList, add a new box to the database.
                            {
                                Box newBox = new Box()
                                {
                                    BoxGrossWeight = box.BoxGrossWeight,
                                    //BoxID = model.BoxID,
                                    BoxStrength = box.BoxStrength,
                                    BoxType = box.BoxType,
                                    BoxWeight = box.BoxWeight,
                                    Compliment = box.Compliment,
                                    Description = box.Description,
                                    InsideBreadth = box.InsideBreadth,
                                    InsideHeight = box.InsideHeight,
                                    InsideLength = box.InsideLength,
                                    OutsideBreadth = box.OutsideBreadth,
                                    OutsideHeight = box.OutsideHeight,
                                    OutsideLength = box.OutsideLength,
                                    UpdateDateTime = DateTime.Now
                                };
    
                                dbEntities.Boxes.AddObject(newBox);
                            }
    
                            //Save changes then redirect to page where user can see the results of the upload.
                            dbEntities.SaveChanges();
                            return RedirectToAction("Index");
                        }
                        else
                        {
                            ViewBag.MsgText = "There are no boxes in this file!";
                            ViewBag.MsgColor = "Red";
                            return View();
                        }
                    }
                    catch
                    {
                        ViewBag.MsgText = "There was an error while updating the database!";
                        ViewBag.MsgColor = "Red";
                    }
                }
                else
                {
                    ViewBag.MsgText = "The file must be an xml file!";
                    ViewBag.MsgColor = "Red";
                }
            }
            else
            {
                ViewBag.MsgText = "You must attach a file!";
                ViewBag.MsgColor = "Red";
            }
    
            return View();
        }
    

    请帮忙! :(

2 个答案:

答案 0 :(得分:1)

首先,如果这应该是&#34;更新&#34; (通过删除和插入新集合),确保在单个事务中进行删除和插入 - 因此要么完整,要么没有任何更改。在实体框架中,您只需在修改实体的所有语句之后调用SaveChanges一次即可。或者您可以明确地使用TransactionScope

至于没有插入实体的问题,没有办法从你的代码中分辨出来 - 对我来说,快速审查似乎很好。尝试一些日志记录 - 例如DbContext之前的SaveChanges中已修改状态的实体的日志数。

答案 1 :(得分:1)

看起来您正在将文件路径传递给文件而不是文件内容。所以在本地它说加载c:\ temp \ whatever.xml并且可以找到它但在服务器上该文件不存在。因此,您点击第一个catch块,它将恢复删除所有框,然后成功返回。我将return View();置于该阻止区域内以确定。