我将实体框架6与asp.net mvc 5一起使用,并且在向数据库中插入新记录时,SaveChanges()方法也会产生错误,该错误也可以在下图中看到:
我在Google上搜索了很多,并做了一些变通方法,但没有任何效果,以下是我的创建操作:
[HttpPost]
public ActionResult Create(ImagesVM img, HttpPostedFileBase upload)
{
if (ModelState.IsValid)
{
var filename = "";
var path = "";
//var originalFilename = "";
if (upload != null && upload.ContentLength > 0)
{
//originalFilename = Path.GetFileName(upload.FileName);
if (!Directory.Exists(Server.MapPath("~/Views/Images/Photots/" + img.PlayerId)))
{
Directory.CreateDirectory(Server.MapPath("~/Views/Images/Photots/" + img.PlayerId));
}
filename = img.PlayerId.ToString() + "_" + Guid.NewGuid().ToString();
path = Server.MapPath("~/Views/Images/Photots/" + filename);
}
else
{
ModelState.AddModelError("error", "There was an error with saving the record");
img.Players = new SelectList(db.Players, "Id", "Name");
return View(img);
}
upload.SaveAs(path);
ImageDTO dto = new ImageDTO();
dto.ImageName = filename;
dto.ImageUrl = path;
dto.ThumbnailTitle = img.ThumbnailTitle;
dto.ThumbnailDetails = img.ThumbnailDetails;
dto.PlayerId = img.PlayerId;
db.Images.Add(dto);
db.SaveChanges();
return RedirectToAction("Index");
}
img.Players = new SelectList(db.Players, "Id", "Name");
return View(img);
}
任何帮助都应事先得到赞赏。