title: Entity Framework entity list always null
body: <p>I have the following two models:</p>
  
  <pre><code>public class Note
  {
      [Key]
      [DatabaseGenerated(DatabaseGeneratedOption.None)]
      public string Id { get; private set; }
  
      [Required] public string Creator { get; set; }
  
      public NoteProfile Profile { get; set; }
  
      [Required(AllowEmptyStrings = true)]
      [DisplayFormat(ConvertEmptyStringToNull = false)]
      public string Content { get; set; }
  
      public static Note Create()
      {
          return new Note();
      }
  
      public Note GenerateId()
      {
          this.Id = Guid.NewGuid().ToString();
          return this;
      }
  
      public Note Finalize()
      {
          this.GenerateId();
          this.Profile = NoteProfile.Create().Finalize();
  
          return this;
      }
  }
  </code></pre>
  
  <p>And:</p>
  
  <pre><code>public class User
  {
      [Key]
      [DatabaseGenerated(DatabaseGeneratedOption.None)]
      public string Id { get; private set; }
  
      [ForeignKey("Creator")]
      [Required]
      public List&lt;Note&gt; Notes { get; set; }
  
      public static User Create()
      {
          return new User();
      }
  
      public User GenerateId()
      {
          this.Id = Guid.NewGuid().ToString();
          return this;
      }
  
      public User Finalize()
      {
          this.GenerateId();
          if (this.Notes == null)
          {
              this.Notes = new List&lt;Note&gt;();
          }
  
          return this;
      }
  }
  </code></pre>
  
  <p>My problem is this: Whenever a new instance of <code>User</code> is created and persisted to the database via EF, when I later get the entity back from the DB, the List of <code>Note</code>s is always <code>null</code>.</p>
  
  <p>I've managed to track down the bug to the following method:</p>
  
  <pre><code>public static bool AddUser(Models.API.Requests.POST.User post)
  {
      var entity = User.Create().Finalize();
          List&lt;Note&gt; user1;
          List&lt;Note&gt; user2;
          using (var context = new Context())
          {
              context.Users.Add(entity);
              context.SaveChanges();
              user1 = context.Users.First(user =&gt; user.Id == entity.Id).Notes;
          }
  
          using (var context = new Context())
          {
              user2 = context.Users.First(user =&gt; user.Id == entity.Id).Notes;
          }
  
          return true;
  
      return true;
  }
  </code></pre>
  
  <p>Inspecting <code>user1</code> and <code>user2</code> via the debugger reveals that <code>user1</code>, which was created <em>before</em> the first context was disposed of, is an initialized <code>List&lt;Note&gt;</code> with 0 items, whereas <code>user2</code>, which was created in a new context, is null.</p>
  
  <p><a href="https://i.stack.imgur.com/Rhcut.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Rhcut.png" alt="enter image description here"></a></p>
  
  <p>My context is very simple:</p>
  
  <pre><code>public class Context : DbContext
  {
      public Context(string connectionString) : this(new MySqlConnection(connectionString), false)
      {
      }
  
      public Context(DbConnection existing, bool contextOwnsConfig) : base(existing, contextOwnsConfig)
      {
      }
  
      public DbSet&lt;Note&gt; Notes { get; set; }
  
      public DbSet&lt;User&gt; Users { get; set; }
  }
  </code></pre>
  
  <p>The DB provider is MySQL. Inspecting the tables EF generates in MySQL Workbench reveals that the foreign key is indeed there:</p>
  
  <p><a href="https://i.stack.imgur.com/l2lv5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/l2lv5.png" alt="enter image description here"></a></p>
  
  <p>Adding new instances of <code>Note</code> with their <code>Creator</code> property set equal to my user's <code>Id</code> yields the exact same result. </p>
  
  <p>Why is this happening?</p>