0

I created admin panel where admin can edit and update list of students. Data is correctly edited but I get error when saving.

Error:

A referential integrity constraint violation occurred: The property value(s) of 'ApplicationUser.Id' on one end of a relationship do not match the property value(s) of 'student.ApplicationUserID' on the other end

I think that the problem is because I apply User.id to ApplicationUserID, but I don't know how to resolve it.

My student class is as follows :

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; }    
}

Save ActionResult method is:

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");
}
tereško
  • 58,060
  • 25
  • 98
  • 150

1 Answers1

0

You are updating a ApplicationUserId foreign key value without updating the original ApplicationUser.Id value.

Referential integrity seems to indicate a relationship foreign key constraint violation.

jaredlee.exe
  • 81
  • 1
  • 9
  • I need to read into it. But the long way is loading the primary object and updating it there. I don't think you should be updating the foreign key by itself. That will always give an error. In fact, probably don't allow an edit on it otherwise you will always get this error. It should draw itself from the Primary object. (Relational DB Logic) – jaredlee.exe Oct 01 '15 at 13:47
  • I don't know for sure. But if you reference an object in EF, if the primary object is updated, it should update the references to it. – jaredlee.exe Oct 01 '15 at 13:50
  • http://stackoverflow.com/questions/9606866/cannot-get-relationship-to-update-for-navigation-properties-in-entity-framework – Vishal Patidar Oct 02 '15 at 03:57