C# 无法保存已编辑的对象

C# 无法保存已编辑的对象,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,我创建了管理员面板,管理员可以编辑和更新学生名单。数据已正确编辑,但保存时出错 错误: 发生引用完整性约束冲突:属性 关系一端的“ApplicationUser.Id”值不正确 在上匹配“student.applicationSerID”的属性值 另一端 我认为问题是因为我将User.id应用于ApplicationSerid,但我不知道如何解决它 我的学生班级如下: public class student { public int Id { get; set; } pub

我创建了管理员面板,管理员可以编辑和更新学生名单。数据已正确编辑,但保存时出错

错误:

发生引用完整性约束冲突:属性 关系一端的“ApplicationUser.Id”值不正确 在上匹配“student.applicationSerID”的属性值 另一端

我认为问题是因为我将User.id应用于ApplicationSerid,但我不知道如何解决它

我的学生班级如下:

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; }    
}
保存操作结果的方法是:

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");
}
公共行动结果编辑(学生st)
{
ApplicationDbContext db=新的ApplicationDbContext();
//更新模型(st);
if(ModelState.IsValid)
{
db.Entry(st.State=EntityState.Modified;
db.SaveChanges();
返回操作(“索引”);
}
返回视图(“索引”);
}

您正在更新ApplicationSerid外键值,而不更新原始ApplicationUser.Id值


引用完整性似乎表示关系外键约束冲突。

也许这个答案可以帮助您:我需要深入了解它。但是要加载主要对象并在那里更新它还有很长的路要走。我认为您不应该自行更新外键。这总是会出错。事实上,可能不允许对其进行编辑,否则您将始终出现此错误。它应该从主对象中提取自身。(关系数据库逻辑)我不确定。但是如果您在EF中引用了一个对象,如果主对象被更新,它应该更新对它的引用。