title: Entity Framework ICollection Navigation Properties null only when testing
body: <p>I'm using Entity Framework 6.1.3 in an MVC 5 project, and I'm having issues with ICollection navigation properties being null when unit testing. I am hitting an actual test database in SQL Express, so this may be more like an integration test for you purists. Regardless of what you call it, it is a problem that I would like to solve.</p>
  
  <p>I have read many answers to similar sounding questions, but none of them seem to hit the same problem that I am having here. I understand EF for the most part, I have lazy loading enabled, my classes are public, and I'm using <code>virtual</code> on my navigation properties.</p>
  
  <p>Here is a simplified example of what I am trying to do:</p>
  
  <h2>Models:</h2>
  
  <pre><code>public class Session
  {
      public int Id { get; set; }
      public string Name { get; set; }
  
      // Navigation Property
      public virtual ICollection&lt;File&gt; Files { get; set; }
  }
  
  public class File
  {
      public int Id { get; set; }
      public string Name { get; set; }
  
      public int SessionId { get; set; }
      public virtual Session Session { get; set; }
  }
  </code></pre>
  
  <h2>Test methods:</h2>
  
  <pre><code>[TestMethod]
  public void Test_TotalFileCount1()
  {
      ApplicationDbContext context = new ApplicationDbContext();
  
      // Create session with no files
      var session = new Session() { Name = "Session1" };
      context.Sessions.Add(session);
      context.SaveChanges();
  
      // This line blows up because session.Files == null
      Assert.AreEqual(0, session.Files.Count);
  }
  
  [TestMethod]
  public void Test_TotalFileCount2()
  {
      ApplicationDbContext context = new ApplicationDbContext();
  
      // Create session
      var session = new Session() { Name = "Session2" };
      context.Sessions.Add(session);
      context.SaveChanges();
  
      // Create file for session
      var file = new File() { Name = "File1", Session = session };
      context.Files.Add(file)
      context.SaveChanges();
  
      // This test passes because session.Files is a
      // collection of one file
      Assert.AreEqual(1, session.Files.Count);
  }
  </code></pre>
  
  <p>The first test above fails because <code>session.Files</code> throws an <code>ArgumentNullException</code>. However, when I call this same code in the full MVC application, <code>session.Files</code> is not null and instead is an empty collection with <code>Count = 0</code>. The second test passes because <code>session.Files</code> is a collection of one <code>File</code> as I would expect. The navigation properties are clearly doing what they're supposed to in the second case, but not in the first case.</p>
  
  <p>Why is EF behaving like this?</p>
  
  <p>I was able to get around this problem by initializing <code>Files</code> as an empty list in the constructor. I know I could do this conditionally in the getter instead, but I don't think I should have to do either of these things because it just works when it's running normally.</p>
  
  <pre><code>public Session()
  {
      this.Files = new List&lt;File&gt;();
  }
  </code></pre>
  
  <p>Does anyone have any insight into what is going on here?</p>