title: Insert a entity that contains a collection navigation property
body: <p>I have two classes:</p>
  
  <pre><code>public class Student
  {
      public int StudentID { get; set; }
      public List&lt;Course&gt; Courses { get; set; }
  }
  
  public class Course
  {
      public int CourseID { get; set; }    
      public string Name { get; set; }
  }
  </code></pre>
  
  <p>And a context:</p>
  
  <pre><code>public class DBContext: DbContext
  {
      public DbSet&lt;Student&gt; Students { get; set; }
      public DbSet&lt;Course&gt; Courses { get; set; }
      public DBContext(DbContextOptions&lt;DBContext&gt; options) : base(options) { }
  }
  </code></pre>
  
  <p>Courses and students can exist independently of each other. In other words, I can create a Course first, then create a Student and associate them with the course.</p>
  
  <p>For example:</p>
  
  <pre><code>var options = new DbContextOptionsBuilder&lt;DBContext&gt;().UseSqlite("Data Source=.\\test.db;");
  
  Course course = null;
  
  using(var context = new DBContext(options.Options))
  {
      context.Database.EnsureCreated();
  }
  
  using (var context = new DBContext(options.Options))
  {
      course = context.Courses.Add(new Course { Name = "Algebra" }).Entity;
      context.SaveChanges();
  }
  
  using (var context = new DBContext(options.Options))
  {
      var student = context.Students.Add(new Student { Courses = new List&lt;Course&gt; { course } });
      context.SaveChanges();
  }
  </code></pre>
  
  <p>However, in the last context scope, I get an unique constraint error. I know that some people say that I need to do everything within the same context. But this will be nearly impossible when using, for example, a WebAPI microservice architecture (StudentService and CourseService). </p>
  
  <p>So how do I handle this?</p>