title: Entity Framework returning old data
body: <p>I have a problem with EF not returning the newest data in a 3 layered WPF application, and I suspect it has something to do with how I handle the lifetime of my context. This is the scenario:</p>
  
  <p>There are several repositories wrapped inside a UnitOfWork. There is also one service (MyService), which uses the UnitOfWork. This UnitOfWork must also be called from the UI directly, without passing through a service.</p>
  
  <p>In the ViewModel of my main window at some point I create a new window (using ViewModel first):</p>
  
  <pre><code>var dialog = new DialogViewModel(_eventAggregator, _unitOfWork, Container.Resolve&lt;CarService&gt;());
  </code></pre>
  
  <p>This main window ViewModel has a UnitOfWork, which has been injected in the constructor, and that is passed to the DialogViewModel.</p>
  
  <p>CarService's constructor also needs a UnitOfWork, which is also injected in its constructor:</p>
  
  <pre><code>public CarService(IUnitOfWork unitOfWork){
      _unitOfWork = unitOfWork;
  }
  </code></pre>
  
  <p>When CarService is used in DialogViewModel to make a query to retrieve some data and make some updates, it works fine the first time. However, when the same query is made the next time to retrieve that data, instead of returning the newest modified one it returns the old/cached one. The query using UnitOfWork (inside CarService) looks like this:</p>
  
  <pre><code>var values = _unitOfWork.GarageRepository.GetSomeValues();
  _unitOfWork.GarageRepository.MakeSomeChangesToTheValuesUsingStoredProcedure();
  </code></pre>
  
  <p>The second time this is called, values doesn't contain the newest version of the data; however it has been updated successfully in the DB.</p>
  
  <p>I'm doing DI using Unity, and this is how my container looks like:</p>
  
  <pre><code>public class Container
  {
       public static UnityContainer Container = new UnityContainer();
  
       // Called once in the AppBoostraper, as soon as the GUI application starts 
       public void BuildUp()
       {
            Container.RegisterType&lt;IUnitOfWork, UnitOfWork&gt;();
            Container.RegisterType&lt;ICarService, CarService&gt;();
       }
  }
  </code></pre>
  
  <p>Why isn't the right data being returned, and how can I fix it?</p>