title: EntityFramework doesn't refresh navigation properties
body: <p>I've got a simple relationship</p>
  
  <p><img src="https://i.stack.imgur.com/Wkz4g.jpg" alt="enter image description here"></p>
  
  <p>I've created a simple app with the Model like the above one. Model in the application must be updated every time DB changes. I can get the latest changes by calling GetDBChanges Stored Procedure. (see method T1Elapsed)</p>
  
  <p>here is the app:</p>
  
  <pre><code>class Program
  {
      private static int? _lastDbChangeId;
      private static readonly MASR2Entities Model = new MASR2Entities();
      private static readonly Timer T1 = new Timer(1000);
      private static readonly Timer T2 = new Timer(1000);
      private static Strategy _strategy = null;
  
      static void Main(string[] args)
      {
          using (var ctx = new MASR2Entities())
          {
              _lastDbChangeId = ctx.GetLastDbChangeId().SingleOrDefault();
          }
          _strategy = Model.Strategies.FirstOrDefault(st =&gt; st.StrategyId == 224);
  
          T1.Elapsed += T1Elapsed;
          T1.Start();
  
          T2.Elapsed += T2Elapsed;
          T2.Start();
  
          Console.ReadLine();
      }
  
      static void T2Elapsed(object sender, ElapsedEventArgs e)
      {
          Console.WriteLine("All rules: " + Model.StrategyRules.Count());
          Console.WriteLine("Strategy: name=" + _strategy.Name + " RulesCount=" + _strategy.StrategyRules.Count);
      }
  
      private static void T1Elapsed(object sender, ElapsedEventArgs e)
      {
          T1.Stop();
          try
          {
              using (var ctx = new MASR2Entities())
              {
                  var changes = ctx.GetDBChanges(_lastDbChangeId).ToList();
                  foreach (var dbChange in changes)
                  {
                      Console.WriteLine("DbChangeId:{0} {1} {2} {3}", dbChange.DbChangeId, dbChange.Action, dbChange.TableName, dbChange.TablePK);
                      switch (dbChange.TableName)
                      {
                          case "Strategies":
                              {
                                  var id = Convert.ToInt32(dbChange.TablePK.Replace("StrategyId=", ""));
                                  Model.Refresh(RefreshMode.StoreWins, Model.Strategies.AsEnumerable());
                              }
                              break;
                          case "StrategyRules":
                              {
                                  var id = Convert.ToInt32(dbChange.TablePK.Replace("StrategyRuleId=", ""));
                                  Model.Refresh(RefreshMode.StoreWins, Model.StrategyRules.AsEnumerable());
                              }
                              break;
                      }
                      _lastDbChangeId = dbChange.DbChangeId;
                  }
              }
          }
          catch (Exception ex)
          {
              Console.WriteLine("ERROR: " + ex.Message);
          }
          finally
          {
              T1.Start();
          }
      }
  }
  </code></pre>
  
  <p>When I run it, this is a sample output:</p>
  
  <pre><code>All rules: 222
  Strategy: name=Blabla2 RulesCount=6
  </code></pre>
  
  <p>then I add a row to the child table (Strategy Rule),</p>
  
  <pre><code>DbChangeId:1713 I StrategyRules StrategyRuleId=811
  All rules: 223
  Strategy: name=Blabla2 RulesCount=7
  </code></pre>
  
  <p>and finally, I remove the row from StrategyRules</p>
  
  <pre><code>DbChangeId:1714 D StrategyRules StrategyRuleId=811
  All rules: 222
  Strategy: name=Blabla2 RulesCount=7
  </code></pre>
  
  <p>Why RulesCount is still 7? How I can force EF to refresh "Navigation Property"?</p>
  
  <p>What I am missing here?</p>
  
  <p><strong>---EDIT---</strong> to cover Slauma's answer</p>
  
  <pre><code>case "StrategyRules":
  {
     var id = Convert.ToInt32(dbChange.TablePK.Replace("StrategyRuleId=", ""));
     if (dbChange.Action == "I")
     {
         //Model.Refresh(RefreshMode.StoreWins, Model.StrategyRules.AsEnumerable());       
     }
     else if (dbChange.Action == "D")
     {
        var deletedRule1 = Model.StrategyRules.SingleOrDefault(sr =&gt; sr.Id == id); 
        //the above one is NULL as expected
  
        var deletedRule2 = _strategy.StrategyRules.SingleOrDefault(sr =&gt; sr.Id == id);
        //but this one is not NULL - very strange, because _strategy is in the same context
        //_strategy = Model.Strategies.FirstOrDefault(st =&gt; st.StrategyId == 224);
     }  
  }
  </code></pre>