title: Entity Framework 6.1: navigation properties not loading
body: <p>This is my first time using Entity Framework 6.1 (code first). I keep running into a problem where my navigation properties are null when I don't expect them to be. I've enabled lazy loading.</p>
  
  <p>My entity looks like this:</p>
  
  <pre><code>public class Ask
  {
      public Ask()
      {
          this.quantity = -1;
          this.price = -1;
      }
  
      public int id { get; set; }
      public int quantity { get; set; }
      public float price { get; set; }
  
      public int sellerId { get; set; }
      public virtual User seller { get; set; }
      public int itemId { get; set; }
      public virtual Item item { get; set; }
  }
  </code></pre>
  
  <p>It has the following mapper:</p>
  
  <pre><code>class AskMapper : EntityTypeConfiguration&lt;Ask&gt;
  {
      public AskMapper()
      {
          this.ToTable("Asks");
  
          this.HasKey(a =&gt; a.id);
          this.Property(a =&gt; a.id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
          this.Property(a =&gt; a.id).IsRequired();
  
          this.Property(a =&gt; a.quantity).IsRequired();
  
          this.Property(a =&gt; a.price).IsRequired();
  
          this.Property(a =&gt; a.sellerId).IsRequired();
          this.HasRequired(a =&gt; a.seller).WithMany(u =&gt; u.asks).HasForeignKey(a =&gt; a.sellerId).WillCascadeOnDelete(true);
  
          this.Property(a =&gt; a.itemId).IsRequired();
          this.HasRequired(a =&gt; a.item).WithMany(i =&gt; i.asks).HasForeignKey(a =&gt; a.itemId).WillCascadeOnDelete(true);
      }
  }
  </code></pre>
  
  <p>Specifically, the problem is that I have an <code>Ask</code> object with a correctly set <code>itemId</code> (which does correspond to an <code>Item</code> in the database), but the navigation property <code>item</code> is null, and as a result I end up getting a NullReferenceException. The exception is thrown in the code below, when I try to access <code>a.item.name</code>:</p>
  
  <pre><code>List&lt;Ask&gt; asks = repo.GetAsksBySeller(userId).ToList();
  List&lt;ReducedAsk&gt; reducedAsks = new List&lt;ReducedAsk&gt;();
  
  foreach (Ask a in asks)
  {
      ReducedAsk r = new ReducedAsk() { id = a.id, sellerName = a.seller.username, itemId = a.itemId, itemName = a.item.name, price = a.price, quantity = a.quantity };
      reducedAsks.Add(r);
  }
  </code></pre>
  
  <p>Confusingly, the <code>seller</code> navigation property is working fine there, and I can't find anything I've done differently in the 'User' entity, nor in its mapper.</p>
  
  <p>I have a test which recreates this, but it passes without any problems:</p>
  
  <pre><code>public void canGetAsk()
  {
      int quantity = 2;
      int price = 10;
  
      //add a seller
      User seller = new User() { username = "ted" };
      Assert.IsNotNull(seller);
      int sellerId = repo.InsertUser(seller);
      Assert.AreNotEqual(-1, sellerId);
  
      //add an item
      Item item = new Item() { name = "fanta" };
      Assert.IsNotNull(item);
      int itemId = repo.InsertItem(item);
      Assert.AreNotEqual(-1, itemId);
  
      bool success = repo.AddInventory(sellerId, itemId, quantity);
      Assert.AreNotEqual(-1, success);
  
      //add an ask
      int askId = repo.InsertAsk(new Ask() { sellerId = sellerId, itemId = itemId, quantity = quantity, price = price });
      Assert.AreNotEqual(-1, askId);
  
      //retrieve the ask
      Ask ask = repo.GetAsk(askId);
      Assert.IsNotNull(ask);
  
      //check the ask info
      Assert.AreEqual(quantity, ask.quantity);
      Assert.AreEqual(price, ask.price);
      Assert.AreEqual(sellerId, ask.sellerId);
      Assert.AreEqual(sellerId, ask.seller.id);
      Assert.AreEqual(itemId, ask.itemId);
      Assert.AreEqual(itemId, ask.item.id);
      Assert.AreEqual("fanta", ask.item.name);
  }
  </code></pre>
  
  <p>Any help would be extremely appreciated; this has been driving me crazy for days.</p>
  
  <hr>
  
  <p>EDIT:</p>
  
  <p>The database is SQL Server 2014.  </p>
  
  <p>At the moment, I have one shared context, instantiated the level above this (my repository layer for the db). Should I be instantiating a new context for each method? Or instantiating one at the lowest possible level (i.e. for every db access)? For example:</p>
  
  <pre><code>public IQueryable&lt;Ask&gt; GetAsksBySeller(int sellerId)
  {
      using (MarketContext _ctx = new MarketContext())
      {
          return _ctx.Asks.Where(s =&gt; s.seller.id == sellerId).AsQueryable();
      }
  }
  </code></pre>
  
  <p>Some of my methods invoke others in the repo layer. Would it better for each method to take a context, which it can then pass to any methods it calls?</p>
  
  <pre><code>public IQueryable&lt;Transaction&gt; GetTransactionsByUser(MarketContext _ctx, int userId)
  {
      IQueryable&lt;Transaction&gt; buyTransactions = GetTransactionsByBuyer(_ctx, userId);
      IQueryable&lt;Transaction&gt; sellTransactions = GetTransactionsBySeller(_ctx, userId);
  
      return buyTransactions.Concat(sellTransactions);
  }
  </code></pre>
  
  <p>Then I could just instantiate a new context whenever I call anything from the repo layer: <code>repo.GetTransactionsByUser(new MarketContext(), userId);</code></p>
  
  <p>Again, thanks for the help. I'm new to this, and don't know which approach would be best.</p>