在我的数据库中,我的 FileLocation 是 VarChar(Max) 我不确定是什么导致了这个 EntityValidationErrors -System.Data.Entity.Validation.DbEntityValidationException:一个或多个实体的验证失败。db.SaveChanges() 函数运行时出现此错误;
模型:
public Assignment()
{
this.CourseAvailables = new HashSet<CourseAvailable>();
}
public string AssignmentID { get; set; }
public Nullable<System.DateTime> SubmissionDate { get; set; }
public string Status { get; set; }
public Nullable<decimal> Mark { get; set; }
public string Comments { get; set; }
public string FileLocation { get; set; }
public virtual ICollection<CourseAvailable> CourseAvailables { get; set; }
}
控制器:
[HttpPost]
public ActionResult Create(Assignment assignment)
{
if (ModelState.IsValid)
{
if (Request.Files.Count > 0)
{
HttpPostedFileBase assignmentFile = Request.Files[0];
if (assignmentFile.ContentLength > 0)
{
var fileName = Path.GetFileName(assignmentFile.FileName);
assignment.FileLocation = Path.Combine(Server.MapPath("~/Content/Image"), fileName);
assignmentFile.SaveAs(assignment.FileLocation);
}
}
db.Assignments.Add(assignment);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(assignment);
}
看法:
<% using (Html.BeginForm("Create", "Assignment", FormMethod.Post, new { enctype = "multipart/form-data" }))
<%: Html.TextBoxFor(model => model.FileLocation, new { type="file"})%>
<%: Html.ValidationMessageFor(model => model.FileLocation) %>