title: Entity Framework adding item to ICollection error
body: <p>I have two simple classes, <code>User</code> and <code>Task</code>:</p>
  
  <pre><code>class User
  {
      public int UserId { get; set; }
      public string Name { get; set; }
      public string Email { get; set; }
  }
  
  class Task
  {
      public int TaskId { get; set; }
      public string Name { get; set; }
      public DateTime CreatedAt { get; set; }
      public virtual ICollection&lt;User&gt; Followers { get; set; }
  }
  </code></pre>
  
  <p>The Task class has a property <code>Followers</code> which is an <code>ICollection&lt;User&gt;</code></p>
  
  <p>Here is the db context class:</p>
  
  <pre><code>class MyContext : DbContext
      {
          public DbSet&lt;User&gt; Users { get; set; }
          public DbSet&lt;Task&gt; Tasks { get; set; }
  
          protected override void OnModelCreating(DbModelBuilder mb)
          {
              mb.Entity&lt;User&gt;().HasKey(u =&gt; u.UserId); 
              mb.Entity&lt;Task&gt;().HasKey(t =&gt; t.TaskId);
          }
      }
  </code></pre>
  
  <p>and here is the code in the Main program:</p>
  
  <pre><code>var db = new MyContext();
  
  var user = new User();
  user.Name = "John Doe";
  user.Email = "jd@email.com";
  db.Users.Add(user);
  db.SaveChanges();
  
  var follower = db.Users.Where(u =&gt; u.Name == "John Doe").FirstOrDefault();
  
  var task = new Task();
  task.Name = "Make the tea";
  task.CreatedAt = DateTime.Now;
  task.Followers.Add(follower);  // ERROR
  
  db.Tasks.Add(task);
  
  db.SaveChanges();
  </code></pre>
  
  <p>The trouble is I am getting an error when trying to add the follower to the task.</p>
  
  <p><code>Object reference not set to an instance of an object.</code></p>
  
  <p>What am I doing wrong?</p>