0

I'm using SQL Server to fetch some data from and making a few changes to it via the below mentioned method in the Controller.

Everything works fine except when db.savechanges() is encountered which always throws a System.Data.Entity.Validation.DbEntityValidationException. I've tried searching for some solutions, but nothing has worked so far on my project. Is there any different approach I should try? What am I doing wrong here?

public ActionResult Details(int a)
        {
            var details = db.tableXes.ToList();
            List<Class1> result = new List<Class1>();
            tableX tb = new tableX();
            foreach (var item in details)
            {
                result.Add(new Class1 { id = item.id, Name = item.Name, X1 = item.X1, X2 = item.X2, Sum = item.X1 + item.X2 + a, Average = (item.X1 + item.X2 + a) / 3 });
                tb.X1 = item.X1;
                tb.X2 = item.X2;
                tb.Sum = item.X1 + item.X2 + a;
                tb.Average = (item.X1 + item.X2 + a) / 3;
                db.tableXes.Add(tb);
                db.SaveChanges();
            }
            return View(db.tableXes.ToList());
        }
  • what's the exception message? – pijemcolu Jun 29 '17 at 10:07
  • An exception of type 'System.Data.Entity.Validation.DbEntityValidationException' occurred in EntityFramework.dll but was not handled in user code. Additional information: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. –  Jun 29 '17 at 10:09
  • Let's look at ... 'EntityValidationErrors' property for more details. – pijemcolu Jun 29 '17 at 10:10
  • it shows count=1 –  Jun 29 '17 at 10:13
  • Long story cut short there's something wrong with your object, it doesn't pass the validation tests and entity framework has no clue how to deal with it. This one should help https://stackoverflow.com/questions/7795300/validation-failed-for-one-or-more-entities-see-entityvalidationerrors-propert – pijemcolu Jun 29 '17 at 10:14
  • You can simly go through your validation errors and fix these problems. – S. Spindler Jun 29 '17 at 10:24

0 Answers0