将数据插入数据库的问题:
例如:goods.GoodPhoto =“ / Content / img / imageGood /” + imageName; 替换为:goods.GoodPhoto =“” + imageName; 而且有效。
[HttpPost]
public ActionResult AddGoods(Good goods)
{
HttpPostedFileBase image = Request.Files["image"];
string SavePath = Server.MapPath("~/Content/img/imageGood/");
string imageName = DateTime.Now.ToFileTime().ToString()+image.FileName;
image.SaveAs(Path.Combine(SavePath, imageName));
goods.GoodPhoto = "/Content/img/imageGood/"+imageName;
goods.ShopID = Convert.ToInt32(Session["ShopID"]);
db.Goods.Add(goods);
db.SaveChanges();
return RedirectToAction("Index", "Home");
}
一旦我编写了“ goods.GoodPhoto =“ / Content / img / imageGood /” + imageName;“,然后它失败了。错误消息是:
System.Data.Entity.Validation.DbEntityValidationException:'验证 一个或多个实体失败。有关更多信息,请参见 EntityValidationErrors属性。 '
答案 0 :(得分:0)
添加尝试捕获以查找确切的错误消息
try
{
db.SaveChanges();
}
catch (DbEntityValidationException e)
{
foreach (var valErrors in e.EntityValidationErrors)
{
foreach (var errors in valErrors.ValidationErrors)
{
//here you will get property name and error message
Trace.TraceInformation("Property: {0} Error: {1}",
errors .PropertyName,
errors .ErrorMessage);
}
}
throw;
}