title: Entity Framework 5 - Immediately refresh DbContext after saving changes
body: <p>I have an MVC application that uses Entity Framework 5. In few places I have a code that creates or updates the entities and then have to perform some kind of operations on the updated data. Some of those operations require accessing navigation properties and I can't get them to refresh.</p>
  
  <p>Here's the example (simplified code that I have)</p>
  
  <p><strong>Models</strong></p>
  
  <pre><code>class User : Model
  {
      public Guid Id { get; set; }
      public string Name { get; set; }
  }
  
  class Car : Model
  {
      public Guid Id { get; set; }
      public Guid DriverId { get; set; }
      public virtual User Driver { get; set; }
  
      [NotMapped]
      public string DriverName
      {
          get { return this.Driver.Name; }
      }
  }
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>public CarController
  {
      public Create()
      {
         return this.View();
      }
  
      [HttpPost]
      public Create(Car car)
      {
          if (this.ModelState.IsValid)
          {
              this.Context.Cars.Create(booking);
              this.Context.SaveChanges();
  
              // here I need to access some of the resolved nav properties
              var test = booking.DriverName;
          }
  
          // error handling (I'm removing it in the example as it's not important)
      }
  }
  </code></pre>
  
  <p>The example above is for the Create method but I also have the same problem with Update method which is very similar it just takes the object from the context in <code>GET</code> action and stores it using <code>Update</code> method in <code>POST</code> action.</p>
  
  <pre><code>public virtual void Create(TObject obj)
  {
      return this.DbSet.Add(obj);
  }
  
  public virtual void Update(TObject obj)
  {
      var currentEntry = this.DbSet.Find(obj.Id);
      this.Context.Entry(currentEntry).CurrentValues.SetValues(obj);
      currentEntry.LastModifiedDate = DateTime.Now;
  }
  </code></pre>
  
  <p>Now I've tried several different approaches that I googled or found on stack but nothing seems to be working for me.</p>
  
  <p>In my latest attempt I've tried forcing a reload after calling <code>SaveChanges</code> method and requerying the data from the database. Here's what I've done.</p>
  
  <p>I've ovewrite the <code>SaveChanges</code> method to refresh object context immediately after save</p>
  
  <pre><code>public int SaveChanges()
  {
      var rowsNumber = this.Context.SaveChanges();
      var objectContext = ((IObjectContextAdapter)this.Context).ObjectContext;
      objectContext.Refresh(RefreshMode.StoreWins, this.Context.Bookings);
  
      return rowsNumber;
  }
  </code></pre>
  
  <p>I've tried getting the updated object data by adding this line of code immediately after <code>SaveChanges</code> call in my <code>HTTP</code> <code>Create</code> and <code>Update</code> actions:</p>
  
  <pre><code>car = this.Context.Cars.Find(car.Id);
  </code></pre>
  
  <p>Unfortunately the navigation property is still <code>null</code>. How can I properly refresh the DbContext immediately after modifying the data?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I forgot to originally mention that I know a workaround but it's ugly and I don't like it. Whenever I use navigation property I can check if it's <code>null</code> and if it is I can manually create new DbContext and update the data. But I'd really like to avoid hacks like this.</p>
  
  <pre><code>class Car : Model
  {    
      [NotMapped]
      public string DriverName
      {
          get
          {
              if (this.Driver == null)
              {
                  using (var context = new DbContext())
                  {
                      this.Driver = this.context.Users.Find(this.DriverId);
                  }
              }
  
              return this.Driver.Name;
           }
      }
  }
  </code></pre>