title: EF - Navigation property is null, even after reloading entity, but works when program is restarted
body: <h2>EDIT</h2>
  
  <p>I've done some testing and found out, that the Item navigation property only works when the context is disposed/a new context is created.</p>
  
  <pre><code>        DataContext context = new DataContext();
          Order ord = context.Orders.FirstOrDefault();
          ord.OrderItem.Add(new OrderItem() { ItemId = 8, Quantity = 2, DateCreated = DateTime.Now });
          // At this point, both Order and Item navigation property of the OrderItem are null
  
          context.SaveChanges();
          // After saving the changes, the Order navigation property of the OrderItem is no longer null, but points to the order. However, the Item navigation property is still null.
  
          ord = context.Orders.FirstOrDefault();
          // After retrieving the order from the context once again, Item is still null.
  
          context.Dispose();
          context = new DataContext();
          ord = context.Orders.FirstOrDefault();
          // After disposing of the context and creating a new one, then reloading the order, both Order and Item navigation props are not null anymore
  </code></pre>
  
  <p>Can someone explain this to me?</p>
  
  <h2>EDIT END</h2>
  
  <p>In my program, the user has a list of orders, to which he can add new orders. The user can also add order items to the order, but this isn't working properly atm since the OrderItem -> Item navigation property is null, even after saving and reloading the order.</p>
  
  <pre><code>OrderItem
  ---------
  OrderId
  ItemId
  Quantity
  DateCreated
  ---------
  Item &lt;- navigation property
  Order
  </code></pre>
  
  <p>When the user has made changes to an order and presses the save button, the program gets the data from the view, updates the activeOrder and sends it to the orderModel, adding it as a new order or updating an existing one.</p>
  
  <p>GetOrderDataFromView() ..</p>
  
  <pre><code>        ...
          // Check for existing item, update if match found, add new item if not
          foreach (ItemViewObject oi in orderItems)
          {
              var existingOrderItem = activeOrder.OrderItem.Where(o =&gt; o.ItemId == oi.ItemId).SingleOrDefault();
  
              if (existingOrderItem != null)
              {
                  existingOrderItem.Quantity = oi.Quantity;
              }
              else
              {
                  activeOrder.OrderItem.Add(new OrderItem()
                  {
                      ItemId = oi.ItemId,
                      Quantity = oi.Quantity,
                      DateCreated = DateTime.Now
                  });
              }
  </code></pre>
  
  <p>Then, in the orderModel class ...</p>
  
  <pre><code>    public void AddOrUpdate(Order order)
      {
          if (order.Id == 0)
          {
              context.Orders.Add(order);
          }
  
          context.SaveChanges();
      }
  </code></pre>
  
  <p>Then the order table is updated, triggering an event, which fires the OrderSelectionChanged() method. At this point, the order is reloaded by retrieving it from the orderModel (return context.Orders.Where...) ...</p>
  
  <pre><code>        // Get values from selected order and populate controls
          if (view.OrderTable.SelectedRows.Count != 0)
          {
              OrderViewObject ovm = (OrderViewObject)view.OrderTable.SelectedRows[0].DataBoundItem;
              activeOrder = orderModel.GetById(ovm.OrderId);
  
              PopulateOrderItemTableControl();
  </code></pre>
  
  <p>When the PopulateOrderItemTableControl() method is called, I start running into trouble ..</p>
  
  <pre><code>        foreach (OrderItem oi in activeOrder.OrderItem)
          {
              orderItems.Add(new ItemViewObject(oi));
          }
  </code></pre>
  
  <p>Because when creating a new ItemViewObject, I need to get the item of the orderItem, through it's navigation property. However, the orderItem.Item navigation property is null and I get an exception here ...</p>
  
  <pre><code>    public ItemViewObject(OrderItem orderItem)
      {
          dateCreated = orderItem.DateCreated;
  
          // Retrieve latest item details, with effective date older or equal to the creation date of the order item
          var details = orderItem.Item.ItemDetails.Where(i =&gt; i.DateEffective  &lt;= dateCreated)
                                                          .OrderByDescending(i =&gt; i.DateEffective)
                                                          .FirstOrDefault();
  </code></pre>
  
  <p>I then have to restart the program, but after doing so, the order items load perfectly fine. So this only happens when adding a new order item to an order, saving the order, then reloading the order and trying to display its order items. </p>
  
  <p>Also, this only happens with new items that have not been previously added to the order. So if I create a new item and add it to an order, click the save button, I will get a null exception and need to restart. After restarting, the item loads fine. If I then delete the order item, save the order, re-add the item and save it again, it does not crash.</p>
  
  <p>Hope this makes sense to someone.</p>
  
  <p>Cheers!</p>
  
  <p>Note: I used model first.</p>