每次运行程序时,db.SaveChanges()
都会出现错误!请问,任何人都可以帮忙解决这个问题吗?
[HttpPost]
public ActionResult Create(HttpPostedFileBase picture, form _form)
{
try
{
string ImageName = Path.GetFileName(picture.FileName);
string physicalPath = Server.MapPath("~/images/" + ImageName);
picture.SaveAs(physicalPath);
form new1 = new form();
new1.Name = _form.Name;
new1.Email = _form.Email;
new1.Phone = _form.Phone;
//.......saving picture url......
new1.DataImage = physicalPath;
//Assign for remaining feilds in table in this way.
db.forms.Add(new1);
db.SaveChanges();
}
catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
{
Exception raise = dbEx;
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
string message = string.Format("{0}:{1}",
validationErrors.Entry.Entity.ToString(),
validationError.ErrorMessage);
// raise a new exception nesting
// the current instance as InnerException
raise = new InvalidOperationException(message, raise);
}
}
throw raise;
}
return View();
}
}
答案 0 :(得分:0)
Form对象中可能有null值。
答案 1 :(得分:0)
打开“复数”。问题是您的模型对象使用的是单数名称(Pupil)约定,而在数据库中您使用的是带有s的复数名称(Pupils)。