实体框架删除现有行无效。它发回实体验证错误 [英] Entity framework's remove existing rows is not working. It sending back entity validation error

查看:105
本文介绍了实体框架删除现有行无效。它发回实体验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新/编辑工作完美,但当我删除模型中的一行以将其发送回DB时,它会一直读取错误实体验证错误。为什么?





Update/Edit works perfectly but when I delete one of the rows in the model to send it back to DB, it keeps reading error 'Entity Validation Error'. Why is that?


public void UpdateReportGroup(TReportHeaderModel model)
       {


           if (model.THeaderTitle == null)
           {
               throw new Exception("Report Group Title must be filled in");
           }
           if (model.THeaderTitle.Length <= 0)
           {
               throw new Exception("A Report Group Title must be filled in.");
           }

           using (var connection = new TReportEntitiesConnection())
           {

               var existingParent = connection.THeaders
                    .Include("TReports")
                    .Where(p => p.ID == model.ID)
                    .SingleOrDefault();

               if (existingParent != null)
               { //update parent report group
                   connection.Entry(existingParent).CurrentValues.SetValues(model);

                   //delete children reports
                   foreach (var existingChild in existingParent.TReports.ToList())
                   {

                       if (!model.TReports.Any(c => c.ID == existingChild.ID))
                       {
                           connection.TReport.Remove(existingChild);
                       }

                   }


                   foreach (var childModel in model.TReports)
                   {
                       var existingChilds = existingParent.TReports
                           .Where(c => c.ID == childModel.ID)
                           .SingleOrDefault();


                       //update existing childreports
                       if (existingChilds != null)
                       {
                           //connection.Entry(existingChild).CurrentValues.SetValues(childModel);
                           existingChilds.TReportName = childModel.name;
                           existingChilds.URL = childModel.url;
                           connection.Entry(existingChilds).State = System.Data.Entity.EntityState.Modified;
                       }
                       else
                       {
                           var newChild = new TReport
                           {
                               URL = childModel.url,
                               TReportName = childModel.name,
                           };
                           existingParent.TReports.Add(newChild);
                       }
                   }

               }
                   connection.SaveChanges();

           }
       }





我的尝试:



第一次尝试:无法正常工作



What I have tried:

First try: Did not work

foreach (var existingChild in existingParent.TReports.ToList())
                   {

                       if (!model.TReports.Any(c => c.ID == existingChild.ID))
                       {
                           connection.TReport.Remove(existingChild);
                       }

                   }





第二次尝试:没有工作

connection.TReport.RemoveRange(existingParent.TReports.ToArray());







正在调试它时,它发送两个模型对象为null。那是为什么?




As I was debugging it, it is sending two model objects as null. Why is that?

推荐答案

解决了!我从我的视图中传递了错误的参数。
Solved it! I was passing wrong parameter from my view.


这篇关于实体框架删除现有行无效。它发回实体验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆