title: updating data in many-to-many relationship in entity framework in code first existing database
body: <p>I am working on app and struggling to really understand how to update data in tables where we have intermediate join table (i.e. table to break many-to-many relationship). for example, from following diagram. if I say I want to add record for new student with list of three courses i.e. math, English and computing. how I do that where I have   </p>
  
  <pre><code>public virtual ICollection&lt;StudentCourse&gt; StudentCourses { get; set; } 
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/2JDqA.png" alt="enter image description here"> </p>
  
  <p>another questions; another scenario if I have courses already and of-course I don't want duplication of math, English and computing course title, how I add new instance of student record there??</p>
  
  <h2>Student</h2>
  
  <pre><code> public partial class Student
   {
      public Student()
      {
          this.StudentCourses = new HashSet&lt;StudentCourse&gt;();
      }
  
      public int StudentID { get; set; }
      public string Name { get; set; }
  
      public virtual ICollection&lt;StudentCourse&gt; StudentCourses { get; set; }
  }
  </code></pre>
  
  <h2>Course</h2>
  
  <pre><code> public partial class Course
  {
      public Course()
      {
          this.StudentCourses = new HashSet&lt;StudentCourse&gt;();
      }
  
      public int CourseID { get; set; }
      public string Title { get; set; }
  
      public virtual ICollection&lt;StudentCourse&gt; StudentCourses { get; set; }
  }
  </code></pre>
  
  <h2>Intermediate model</h2>
  
  <pre><code>public partial class StudentCourse
  {
      public int StudentCourseID { get; set; }
      public int StudentID { get; set; }
      public int CourseID { get; set; }
  
      public virtual Course Course { get; set; }
      public virtual Student Student { get; set; }
  }
  </code></pre>
  
  <p>My third question is, do I have to use Virtual key word in above code.</p>