title: EntityFramework Code First: Why does Lazy Loading of Many to Many Reference fail?
body: <p>My EF 4.1 Code First model should be fairly self-explanitory if you look at the code below and am trying to use DataAnnotations whenever I can.</p>
  
  <p>A franchise sells many items.
  A franchise has many stores that can sell the items.
  A store may be sold out of an item.</p>
  
  <p><b>A many to many table called SoldOutItems allows for a store to have many sold out items, and an item to be sold out at many stores.</b></p>
  
  <p><h3>Problem:</h3> The navigation properties associated with the many to many table won't lazy load for me like a normal navigation property would. It was possible to do when I used EntityFramework in a Database first approach with the same exact database that generated by the model below.</p>
  
  <p>I assume I'm just doing something wrong with the [InverseProperty] DataAnnotations, but everything I search for online just states that as long as the navigation property is virtual, it should be 'lazy loadable'.</p>
  
  <p>At the very bottom, I've provided code to reproduce the issue in a console app.</p>
  
  <p><b>Franchises</b></p>
  
  <pre><code>[Table("Franchises")]
  public class Franchise
  {
      [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
      public Int64 Id { get; set; }
      [Required, MaxLength(50)]
      public string Name { get; set; }
  
      [InverseProperty("Franchise")]
      public virtual ICollection&lt;Store&gt; Stores { get; set; }
  
      [InverseProperty("Franchise")]
      public virtual ICollection&lt;Item&gt; Items { get; set; }
  }
  </code></pre>
  
  <p><b>Stores</b></p>
  
  <pre><code>[Table("Stores")]
  public class Store
  {
      [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
      public Int64 Id { get; set; }
      public Int64 FranchiseId { get; set; }
      [Required, MaxLength(50)]
      public string Name { get; set; }
  
      [ForeignKey("FranchiseId")]
      public virtual Franchise Franchise { get; set; }
  
      public virtual ICollection&lt;Item&gt; UnavailableItems { get; set; }
  }
  </code></pre>
  
  <p><b>Items</b></p>
  
  <pre><code>[Table("Items")]
  public class Item
  {
      [Key]
      public Int64 Id { get; set; }
      public Int64 FranchiseId { get; set; }
      [Required, MaxLength(50)]
      public string Name { get; set; }
  
      [ForeignKey("FranchiseId")]
      public virtual Franchise Franchise { get; set; }
  
      public virtual ICollection&lt;Store&gt; StoresUnavailableAt { get; set; }
  }
  </code></pre>
  
  <p><b>SoldOutItems</b></p>
  
  <pre><code>[Table("SoldOutItems")]
  public class SoldOutItem
  {
      [Key, Column(Order = 0)]
      public Int64 StoreId { get; set; }
      [Key, Column(Order = 1)]
      public Int64 ItemId { get; set; }
  
      [InverseProperty("UnavailableItems")]
      [ForeignKey("StoreId")]
      public virtual Store Store { get; set; }
  
      [InverseProperty("StoresUnavailableAt")]
      [ForeignKey("ItemId")]
      public virtual Item Item { get; set; }
  }
  </code></pre>
  
  <p><b>Context</b></p>
  
  <pre><code>public class RetailContext : DbContext
  {
      public DbSet&lt;Franchise&gt; Franchises { get; set; }
      public DbSet&lt;Store&gt; Stores { get; set; }
      public DbSet&lt;Item&gt; Items { get; set; }
      public DbSet&lt;SoldOutItem&gt; SoldOutItems { get; set; }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          modelBuilder.Entity&lt;SoldOutItem&gt;().HasRequired(s =&gt; s.Store).WithMany().WillCascadeOnDelete(false);
          base.OnModelCreating(modelBuilder);
      }
  }
  </code></pre>
  
  <p><b>Code to reproduce null exception that gets thrown: </b></p>
  
  <pre><code>static void Main(string[] args)
  {
      RetailContext ctx = new RetailContext();
      ctx.Configuration.LazyLoadingEnabled = true;
  
      Franchise franchise = ctx.Franchises.Where(f =&gt; f.Id == 0).FirstOrDefault();
      if (franchise == null)
      {
          franchise = new Franchise() { Id = 0, Name = "My Franchise" };
          ctx.Franchises.Add(franchise);
          ctx.SaveChanges();
      }
  
      Item item = ctx.Items.Where(i =&gt; i.Name == "Item 1").FirstOrDefault();
      if (item == null)
      {
          item = new Item() { Name = "Item 1" };
          franchise.Items.Add(item);
      }
  
      Store myStore = ctx.Stores.Where(s =&gt; s.Id == 0).FirstOrDefault();
      if (myStore == null)
      {
          myStore = new Store() { Id = 1, Name = "My Store" };
          myStore.UnavailableItems.Add(item); // Exception: UnavailableItems is null
  
      }
  }
  </code></pre>
  
  <h2>Solution</h2>
  
  <p>kwon's answer does indeed fix the immediate problem I was having. However, a much bigger problem for me is that it seems I cannot get what I want simply using DataAnnotations. So I ended up removing my Junction Table (SoldOutItems) in favor of using Fluent. This unfortunately creates a cyclical reference problem since I cannot remove the cascading deletes from my many-to-many table now.</p>
  
  <p>New code after removing my 'SoldOutItems' table:</p>
  
  <pre><code>public class RetailContext : DbContext
  {
      public DbSet&lt;Franchise&gt; Franchises { get; set; }
      public DbSet&lt;Store&gt; Stores { get; set; }
      public DbSet&lt;Item&gt; Items { get; set; }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          modelBuilder.Entity&lt;Store&gt;().HasMany(s =&gt; s.UnavailableItems).WithMany(i =&gt; i.StoresUnavailableAt).Map(mc =&gt; mc.ToTable("SoldOutItems").MapLeftKey("StoreId").MapRightKey("ItemId"));
          //Unfortunately I have to remove the cascading delete here since I can't figure out how to do it on the table created from the line above.
          modelBuilder.Entity&lt;Franchise&gt;().HasMany(f =&gt; f.Stores).WithRequired(s =&gt; s.Franchise).WillCascadeOnDelete(false);
          base.OnModelCreating(modelBuilder);
      }
  }
  </code></pre>