db.SaveChanges()出错;类型' System.Data.Entity.Infrastructure.DbUpdateException'的例外情况发生在EntityFramework.dll中没有处理

时间:2017-06-12 09:55:07

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

类型' System.Data.Entity.Infrastructure.DbUpdateException'的例外情况发生在EntityFramework.dll中但未在用户代码中处理 //控制器代码  从表单集合中获取数据是因为我们通过使用循环来获取大量文本框。

然后将其放在下表TargetManagement_monthly Target的模型类属性中 在调试中,它给出了innerException "无效的对象名称' PT.TargetManagement_MonthlyTarget'。"}

 [HttpPost]
                public ActionResult CreateTarget([Bind(Include =
        "MonthlyTargetID,FinancialYearID,MonthID,dvn_code,itm_ItemCode,ChannelCustomerCode,ChannelCustomerName,IsChannel,TargetQuantity,TargetRevenue,CreatedBy,CreatedDate,UpdatedBy,UpdatedDate")]FormCollection ct)
                {
                    if (ModelState.IsValid)
                    {
                        for (int i = 0; i <= 0; i++)
                        {
//Making an object of required table calss.
                            TargetManagement_MonthlyTarget tm = new TargetManagement_MonthlyTarget();

//Now get the formcollection data and Add it to model class properties one by one.
                            tm.FinancialYearID = int.Parse(ct["FinancialYearID"]);
                            tm.dvn_code = ct["dvn_code"];
                            tm.ChannelCustomerCode = ct["ChannelCustomerCode"];
                            tm.ChannelCustomerName = ct["ChannelCustomerName"];
                            if (ct["IsChannel"] == "customer") { tm.IsChannel = false; } else { tm.IsChannel = true; }

                            int count = int.Parse(Request["hfTotalRecordCount"]);
                            string[] arr = new string[count];
                            arr[i] = ct["lstMonthlyTargetDetailModel[" + i + "].itm_ItemCode"];
                            tm.itm_ItemCode = arr[i];
                            for (int j = 1; j <= 1; j++)
                            {
                                int a = int.Parse((Request.Form["lstMonthlyTargetDetailModelColumn[" + j + "].FirstMonthTargetQuantityRow[" + i + "]"]));
                                int b = int.Parse((ct["lstMonthlyTargetDetailModelColumn[" + j + "].FirstMonthTargetRevenueRow[" + i + "]"]));
                                if (a != 0 || a != -1) { tm.TargetQuantity = a; }
                                if (b != 0 || b != -1) { tm.TargetRevenue = b; }
                                tm.MonthID = j;
                                DateTime dt = DateTime.Today; DateTime FDate;
                                var d = String.Format("{0:u}", dt); DateTime.TryParse(d, out FDate);
                                tm.CreatedBy = 16; tm.CreatedDate = FDate; tm.UpdatedBy = 16; tm.UpdatedDate = FDate;
    //MonthlyTargetID is primary key of the table
                                db.TargetManagement_MonthlyTarget.Add(new TargetManagement_MonthlyTarget {FinancialYearID=tm.FinancialYearID,
                                    MonthID=tm.MonthID,dvn_code=tm.dvn_code,
                                    itm_ItemCode=tm.itm_ItemCode,
                                    ChannelCustomerCode=tm.ChannelCustomerCode,
                                    ChannelCustomerName=tm.ChannelCustomerName,
                                    IsChannel=tm.IsChannel,
                                    TargetQuantity=tm.TargetQuantity,
                                    TargetRevenue=tm.TargetRevenue,
                                    CreatedBy=tm.CreatedBy,
                                    CreatedDate=tm.CreatedDate,
                                    UpdatedBy=tm.UpdatedBy,
                                    UpdatedDate=tm.UpdatedDate });

                            }

                        }
                        db.SaveChanges();
                    }

                    return RedirectToAction("Target");
                }

0 个答案:

没有答案