title: Entity Framework caching in aspnet Identity
body: <p>I'm building a project in EF6 and aspnet Identity.</p>
  
  <p>I'm facing the following problem:</p>
  
  <p>if I call </p>
  
  <pre><code>var account = await FindByNameAsync(userName); // account.IsConfirmed = true
  </code></pre>
  
  <p>I get the account I'm looking for (example: isConfirmed = true).</p>
  
  <p>When I manually change a value in my database (isConfirmed = true -> isConfirmed = false) and I run my query again, I still get my old account object (isConfirmed = true)</p>
  
  <pre><code>var account = await FindByNameAsync(userName); // Should be account.IsConfirmed = false, but still gives me IsConfirmed = true
  </code></pre>
  
  <p>I've tried adding the following in my DbContext constructor</p>
  
  <pre><code>&gt; this.Configuration.ProxyCreationEnabled = false;
  &gt; this.Configuration.LazyLoadingEnabled = false;
  </code></pre>
  
  <p>But this didn't change anything.</p>
  
  <p>What can I do about this? How long does the cached data remain?
  All the posts I've seen, require you to run the query (from .. in ..), but seeing how I'm using aspnet Identity and I have no control over these things, what can I do?</p>
  
  <p>Thanks!</p>
  
  <p>EDIT: added dbContext info</p>
  
  <p>My IoC (Unity)</p>
  
  <pre><code>container.RegisterType&lt;IUnitOfWork, UserManagementContext&gt;(new HttpContextLifetimeManager&lt;IUnitOfWork&gt;());
  container.RegisterType&lt;IUserStore&lt;Account&gt;, UserStore&lt;Account&gt;&gt;(new InjectionConstructor(container.Resolve&lt;IUnitOfWork&gt;()));
  </code></pre>
  
  <p>HttpContextLifeTimeManager:</p>
  
  <pre><code>public class HttpContextLifetimeManager&lt;T&gt; : LifetimeManager, IDisposable
  {
      public override object GetValue()
      {
          return HttpContext.Current.Items[typeof(T).AssemblyQualifiedName];
      }
  
      public override void SetValue(object newValue)
      {
          HttpContext.Current.Items[typeof(T).AssemblyQualifiedName] = newValue;
      }
  
      public override void RemoveValue()
      {
          HttpContext.Current.Items.Remove(typeof(T).AssemblyQualifiedName);
      }
  
      public void Dispose()
      {
          RemoveValue();
      }
  }
  </code></pre>
  
  <p>My IUnitOfWork</p>
  
  <pre><code>public interface IUnitOfWork : IDisposable
  {
      void Save();
      Task SaveAsync();
      DbSet&lt;TEntity&gt; EntitySet&lt;TEntity&gt;() where TEntity : class;
      void MarkAsModified&lt;TEntity&gt;(TEntity entity) where TEntity : class;
  }
  </code></pre>
  
  <p>My UserManagementContext</p>
  
  <pre><code>public class UserManagementContext : IdentityDbContext&lt;Account&gt;, IUnitOfWork
  {
      static UserManagementContext()
      {
          //Database.SetInitializer&lt;UserManagementContext&gt;(new RecreateDatabase());
          Database.SetInitializer&lt;UserManagementContext&gt;(null);
      }
  
      public UserManagementContext()
          : base("Name=UserManagementConnection")
      {
          this.Configuration.ProxyCreationEnabled = false;
          this.Configuration.LazyLoadingEnabled = false;
      }
  
      // ... (my Dbsets)
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          // configuration ..
      }
  
      public void Save()
      {
          SaveChanges();
      }
  
      public async Task SaveAsync()
      {
          await SaveChangesAsync();
      }
  
      public DbSet&lt;TEntity&gt; EntitySet&lt;TEntity&gt;() where TEntity : class
      {
          return this.Set&lt;TEntity&gt;();
      }
  
      public void MarkAsModified&lt;TEntity&gt;(TEntity entity) where TEntity : class
      {
          this.Entry(entity).State = EntityState.Modified;
      }
  }
  </code></pre>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I discovered another strange thing. When I set my last login date field, that change gets picked up, but when I set my isConfirmed field, that doesn't get picked up.. (the DB change actually gets overwriten by the cached data!</p>
  
  <p>So this confirms that data entered via code get persisted, but manual changes in the DB get ignored.</p>
  
  <p><strong>UPDATE 2</strong>
  In case anyone has this problem as well: the problem was not aspnet Identity, it's EF.</p>
  
  <p>What I did was implemented my own userstore and manually accessed EF and used .AsNoTracking() to avoid the caching.</p>