title: one of my entities isn't saving in entity framework when I update through my repository
body: <p>I have two entities 'Student' and 'StudentImage'. I'm using MVC 5 and I have a form where the student fills out his/her profile with an image and then saves it to the student and studentimage table. When there is no previous student I can insert both the student and studentimage ok, but when my repository does an update only the student gets updated and the studentimage does NOT get updated.
  *I've done a sql profiler check and it doesn't look like studentimage gets used in any of the update queries. Another thing is that it's not throwing any error. It finishes and returns to my view.</p>
  
  <p>here are my entities</p>
  
  <pre><code>public class Student
  {
      public int StudentId { get; set; }
  
      [Index(IsUnique=true)]
      [Required]
      [MaxLength(128)]
      public string AspNetUserRefId { get; set; }
  
      public virtual StudentImage StudentImage { get; set; } //one-to-one
  
      // other members below
  }
  
  public class StudentImage
  {
      [Key, ForeignKey("Student")]
      public int StudentId { get; set; }
  
      public byte[] Image { get; set; }
  
      public virtual Student Student { get; set; } // one-to-one
  }
  </code></pre>
  
  <p>here is my update method in the repository for my student</p>
  
  <pre><code>public void InsertOrUpdate(Student student)
  {
      if (!context.Students.Any(u =&gt; u.AspNetUserRefId == student.AspNetUserRefId))
      {
          // New entity
          context.Students.Add(student);
      }
      else
      {
          // Existing entity
          context.Entry(student).State = EntityState.Modified;
      }
  }
  </code></pre>
  
  <p>Here is my posted controller method edit that does the saving</p>
  
  <pre><code>[HttpPost]
      public ActionResult Edit(EditStudentViewModel studentViewModel)
      {
          if (ModelState.IsValid)
          {
              byte[] array;
              using (MemoryStream ms = new MemoryStream())
              {
                  studentViewModel.StudentImageFileBase.InputStream.CopyTo(ms);
                  array = ms.GetBuffer();
              } 
  
              var student = new Student
              {
                  AspNetUserRefId = studentViewModel.AspNetUserRefId,
                  CatchPhrase = studentViewModel.CatchPhrase,
                  StartedPracticing = Convert.ToInt16(studentViewModel.SelectedYearId),
                  Location = studentViewModel.Location,
                  Education = studentViewModel.Education,
                  Work = studentViewModel.Work,
                  SelfDescription = studentViewModel.SelfDescription,
                  Inspirations = studentViewModel.Inspirations,
                  StudentId = studentViewModel.StudentId,
                  StudentImage = new StudentImage { Image = array, StudentId = studentViewModel.StudentId}
              };
  
              studentRepository.InsertOrUpdate(student);
              studentRepository.Save();
              return RedirectToAction("Edit", "Student");
          } else {
              return View();
          }
      }
  </code></pre>