更新条目时发生错误。有关详细信息,请参见内部异常-ASP.NET MVC 5

时间:2019-03-25 14:55:55

标签: asp.net-mvc entity-framework-6

我将实体框架6与asp.net mvc 5一起使用,并且在向数据库中插入新记录时,SaveChanges()方法也会产生错误,该错误也可以在下图中看到:

enter image description here

我在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);

    }  

任何帮助都应事先得到赞赏。

0 个答案:

没有答案