title: Unexpected InvalidOperationException when trying to change relationship via property default value
body: <p>In the sample code below I get the following exception when doing <code>db.Entry(a).Collection(x =&gt; x.S).IsModified = true</code>:</p>
  
  <blockquote>
    <p>System.InvalidOperationException: 'The instance of entity type 'B'
    cannot be tracked because another instance with the key value '{Id:
    0}' is already being tracked. When attaching existing entities, ensure
    that only one entity instance with a given key value is attached.</p>
  </blockquote>
  
  <p>Why doesn't it add instead of attach the instances of B?</p>
  
  <p>Strangely the documentation for <code>IsModified</code> doesn't specify <code>InvalidOperationException</code> as a possible exception. Invalid documentation or a bug?</p>
  
  <p>I know this code is strange, but I wrote it only to understand how ef core works in some weird egde cases. What I want is an explanation, not a work around.</p>
  
  <pre><code>using Microsoft.EntityFrameworkCore;
  using Microsoft.Extensions.Logging;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  
  class Program
  {
      public class A
      {
          public int Id { get; set; }
          public ICollection&lt;B&gt; S { get; set; } = new List&lt;B&gt;() { new B {}, new B {} };
      }
  
      public class B
      {
          public int Id { get; set; }
      }
  
      public class Db : DbContext {
          private const string connectionString = @"Server=(localdb)\mssqllocaldb;Database=Apa;Trusted_Connection=True";
  
          protected override void OnConfiguring(DbContextOptionsBuilder o)
          {
              o.UseSqlServer(connectionString);
              o.EnableSensitiveDataLogging();
          }
  
          protected override void OnModelCreating(ModelBuilder m)
          {
              m.Entity&lt;A&gt;();
              m.Entity&lt;B&gt;();
          }
      }
  
      static void Main(string[] args)
      {
          using (var db = new Db()) {
              db.Database.EnsureDeleted();
              db.Database.EnsureCreated();
  
              db.Add(new A { });
              db.SaveChanges();
          }
  
          using (var db = new Db()) {
              var a = db.Set&lt;A&gt;().Single();
              db.Entry(a).Collection(x =&gt; x.S).IsModified = true;
              db.SaveChanges();
          }
      }
  }
  </code></pre>