title: EF 6.1.3 how to seed on-to-many relationship
body: <p>I am trying to seed my EF code first database in the Configuration file of the migrations folder, but somehow I have no idea how as I do not want to add a new entry to the related database.</p>
  
  <p>I have the following database structure.</p>
  
  <pre><code>public class ClassSchedule
      {
          public int Id { get; set; }
          public DateTime StartTime { get; set; }
          public DateTime EndTime { get; set; }
  
          public virtual Class Classes { get; set; }
          public virtual Classroom Classrooms { get; set; }
      }
  
  public class Class
      {
          public Class()
          {
              this.Students = new HashSet&lt;Student&gt;();
              this.ClassSchedules = new HashSet&lt;ClassSchedule&gt;();
          }
  
          public int Id { get; set; }
          public int LevelId { get; set; }
  
          [StringLength(50)]
          public string ClassName { get; set; }
          public int MaxStudents { get; set; }
  
          public virtual Level Levels { get; set; }
          public ICollection&lt;Student&gt; Students { get; set; }
          public ICollection&lt;ClassSchedule&gt; ClassSchedules { get; set; }
      }
  
    public class Classroom
      {
          public Classroom()
          {
              this.ClassSchedules = new HashSet&lt;ClassSchedule&gt;();
          }
  
          public int Id { get; set; }
          public string ClassRoomName { get; set; }
          public int MaxStudents { get; set; }
  
          public ICollection&lt;ClassSchedule&gt; ClassSchedules { get; set; }
      }
  </code></pre>
  
  <p>And have got the following Code in my migration configuration file: </p>
  
  <pre><code>// all classrooms
    context.Classrooms.Add(new Classroom() { ClassRoomName = "Cherry", MaxStudents = 20 });
    context.Classrooms.Add(new Classroom() { ClassRoomName = "Pear", MaxStudents = 12 });
    context.Classrooms.Add(new Classroom() { ClassRoomName = "Apple", MaxStudents = 12 });
  
  
    // classes
    context.Classes.Add(new Class() { ClassName = "YR1", MaxStudents = 12, LevelId = 1 });
    context.Classes.Add(new Class() { ClassName = "YB1", MaxStudents = 6, LevelId = 3 });
    context.Classes.Add(new Class() { ClassName = "IELTS L1", MaxStudents = 40, LevelId = 14 });
  </code></pre>
  
  <p>Now what i want to do is something like this: </p>
  
  <pre><code>// ClassRoom schedules, links the class and classroom on a certain time
              context.ClassSchedules.Add(new ClassSchedule() { StartTime = DateTime.Parse("2017-09-17T18:00:00"), EndTime = DateTime.Parse("2017-09-17T20:00:00"), Classes = 1, Classrooms = 1 });
  </code></pre>
  
  <p>Another thing I tried was: </p>
  
  <pre><code>   context.ClassSchedules.Add(new ClassSchedule() { StartTime = DateTime.Parse("2017-09-17T18:00:00"), EndTime = DateTime.Parse("2017-09-17T20:00:00"), Classes = { Id = 1 }, Classrooms = { Id = 2 } });
  </code></pre>
  
  <p>However that does't work, also note that I cannot use <code>new Classes() { Id = 1 }</code>  as this will create a new entity.</p>
  
  <p>Any help will be appreciated, Cheers!</p>