title: Entity framework reload entity from context
body: <p>I have the following scenario:</p>
  
  <p>I am using EF with Repository pattern and Unit Of Work from <strong><a href="http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application" rel="nofollow noreferrer">here</a></strong></p>
  
  <p>Then I extended my partial class and have created a Finder class which have GetAll and other methods.
  You can this see below:</p>
  
  <p><img src="https://i.stack.imgur.com/xgS1n.png" alt="enter image description here"></p>
  
  <p>Below you can see that I am using a unit of work class with repositories of all classes to get the instance from the generic repository:</p>
  
  <p><img src="https://i.stack.imgur.com/viTbg.png" alt="enter image description here"></p>
  
  <pre><code>protected Repository&lt;Category&gt; _CategoryRepository;
  public Repository&lt;Category&gt; CategoryRepository
  {
      get { return _CategoryRepository ?? (_CategoryRepository = new Repository&lt;Category&gt;(context)); }
  }
  </code></pre>
  
  <p>So in this way I had different repositories and when getting an entity from db and updating it from a different context caused problems. So I used <strong><a href="http://www.west-wind.com/weblog/posts/2008/Feb/05/Linq-to-SQL-DataContext-Lifetime-Management" rel="nofollow noreferrer">this</a></strong> method to use for <em>context lifetime management</em>. And it resolved that issue.</p>
  
  <p>Now the problem I am facing is in the following code:</p>
  
  <pre><code>var cat = Instance.Category.GetSingle(c =&gt; c.CategoryID == 7);
  
  var orignal = cat.CategoryName;
  
  var expected = cat.CategoryName + " Test Catg Update";
  
  cat.CategoryName = expected;
  
  cat.Update(); //This doesn't actually update due to a validation in place (which is correct)
  
  cat = Instance.Category.GetSingle(c =&gt; c.CategoryID == 7);
  
  Assert.AreEqual(cat.CategoryName, expected);
  </code></pre>
  
  <p>When I use Update and I have some validators in it, and the Update fails (for example due to size of the string exceeds 15 characters). When I try to call GetSingle (2nd last line of above code) again, it brings me the same old record which is [cat.CategoryName + " Test Catg Update"]. Is this a normal way? If not how can this be fixed so I can reload the object from database.</p>
  
  <p>Let me know if you need any other code or reference.</p>