【发布时间】:2015-10-01 13:26:30
【问题描述】:
我创建了管理员面板,管理员可以在其中编辑和更新学生列表。数据已正确编辑,但保存时出错。
错误:
发生了参照完整性约束违规:属性 关系一端的“ApplicationUser.Id”的值不 匹配“student.ApplicationUserID”的属性值 另一端
我认为问题是因为我将User.id应用于ApplicationUserID,但我不知道如何解决它。
我的学生班级如下:
public class student
{
public int Id { get; set; }
public string FirstName { get; set; }
public string SecondName { get; set; }
public string ClassName { get; set; }
public virtual ApplicationUser Users { get; set; }
public string ApplicationUserID { get; set; }
}
保存ActionResult的方法是:
public ActionResult Edit(student st)
{
ApplicationDbContext db = new ApplicationDbContext();
//UpdateModel<istudent>(st);
if (ModelState.IsValid)
{
db.Entry(st).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View("Index");
}
【问题讨论】:
-
也许这个答案可以帮助你:stackoverflow.com/a/11596430/858757
标签: c# asp.net-mvc entity-framework