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());
}