title: What binds ApplicationUser to the aspnetusers table in Database First?
body: <p>I have a database-first EDMX model in a separate library (e.g. <code>Common.Feedback.Data</code>), that includes the <code>AspNetUser</code> table and its related Identity Framework tables (pulled from another existing, working, database/application).</p>
  
  <p>I have updated the <code>ApplicationDbContext</code> connection string to point to the new model and new database connection:</p>
  
  <pre><code>using System.Data.Entity;
  using System.Security.Claims;
  using System.Threading.Tasks;
  using Microsoft.AspNet.Identity;
  using Microsoft.AspNet.Identity.EntityFramework;
  
  namespace Feedback.MvcApplication.Models
  {
      // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
  
      public class ApplicationUser : IdentityUser
      {
          public async Task&lt;ClaimsIdentity&gt; GenerateUserIdentityAsync(UserManager&lt;ApplicationUser&gt; manager)
          {
              // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
              var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
              // Add custom user claims here
              return userIdentity;
          }
      }
  
      public class ApplicationDbContext : IdentityDbContext&lt;ApplicationUser&gt;
      {
          public ApplicationDbContext()
              : base("FeedbackEntities", throwIfV1Schema: false)
          {
          }
  
          public static ApplicationDbContext Create()
          {
              return new ApplicationDbContext();
          }
      }
  }
  </code></pre>
  
  <p>The connection string in the web.config includes the full path to the assembly and that reference is fine:</p>
  
  <pre><code>&lt;add name="FeedbackEntities" 
       connectionString="metadata=res://Common.Feedback.Data/FeedbackModel.csdl|res://Common.Feedback.Data/FeedbackModel.ssdl|res://Common.Feedback.Data/FeedbackModel.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;data source=mydatabase.database.windows.net;initial catalog=Feedback;persist security info=True;user id=LeaveFeedbackuser@mydatabase;password=mypassword;MultipleActiveResultSets=True;App=EntityFramework&amp;quot;" 
       providerName="System.Data.EntityClient" /&gt;
  </code></pre>
  
  <p>At runtime, any access to login results in the following error:</p>
  
  <blockquote>
    <p>"The entity type ApplicationUser is not part of the model for the current context"</p>
  </blockquote>
  
  <p>All links I have tried, relating to that error, usually involve ways of updating the migration.</p>
  
  <p>I cannot enable migrations as it is an EDMX first setup, but I assume there is a binding between the <code>ApplicationUser</code> object and the <code>aspnetusers</code> table "somewhere" behind the scenes.</p>
  
  <p>Does anyone here have a clear understanding of how the <code>ApplicationUser</code> class is mapped to/from the <code>aspnetusers</code> table at runtime and explain how to get my code to work with the database?</p>
  
  <p><strong>Repro steps</strong></p>
  
  <ul>
  <li>Create a new MVC Web Application using VS 2013</li>
  <li>Update all NuGet packages to latest</li>
  <li>Take an existing SQL database with the Identity Framework tables and copy it to a new table (strip out any unrelated tables).</li>
  <li>Add new tables for the project</li>
  <li>Create a class library to hold the data models (e.g. <code>Common.Feedback.Data</code>)</li>
  <li>Add an Edmx data model, to the library, based on the database created previously</li>
  <li>Change the connection string to fully qualify the assembly (not <code>res://*/</code>)</li>
  <li>Change the connection string name in <code>IdentityModel.cs</code> to match the connection string name in the config.</li>
  <li>Copy the connection string from the library's <code>app.config</code> to the web project's <code>web.config</code></li>
  <li>Try login and you will hit the error mentioned</li>
  </ul>
  
  <p><strong>Update:</strong></p>
  
  <p>Based on a stray post, I changed my connection string to match the normal SQL connection the Identity Framework is configured to use by default (and using a Sql client):</p>
  
  <pre><code>&lt;add name="FeedbackSql" 
       connectionString="data source=mydatabase.database.windows.net;initial catalog=Feedback;persist security info=True;user id=LeaveFeedbackuser@mydatabase;password=mypassword;MultipleActiveResultSets=True;App=EntityFramework" 
       providerName="System.Data.SqlClient" /&gt;
  </code></pre>
  
  <p>and changed to setup to use this new connection:</p>
  
  <pre><code>   public ApplicationDbContext()
          : base("FeedbackSql", throwIfV1Schema: false)
      {
      }
  </code></pre>
  
  <p>Bizarrely the error changes to:</p>
  
  <blockquote>
    <p>The magic number in GZip header is not correct. Make sure you are passing in a GZip stream. </p>
  </blockquote>
  
  <p>I think the initial change to a SqlClient provider is correct, so this new error may related to using an Azure database over that connection. I am open to suggestions on what to try next.</p>
  
  <p>Updated web.config based on suggestion by @rism and <a href="http://blogs.msdn.com/b/davidobando/archive/2012/08/14/changing-ef-s-default-provider-from-localdb-to-sql-server.aspx">this link</a> (but the GZip error persists):</p>
  
  <pre><code>  &lt;entityFramework&gt;
      &lt;defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"&gt;
        &lt;parameters&gt;
          &lt;!--&lt;parameter value="mssqllocaldb" /&gt;--&gt;
          &lt;parameter value="data source=mydatabase.database.windows.net;initial catalog=Feedback;persist security info=True;user id=myuserid;password=mypassword;MultipleActiveResultSets=True;App=EntityFramework" /&gt;
        &lt;/parameters&gt;
      &lt;/defaultConnectionFactory&gt;
      &lt;providers&gt;
        &lt;provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /&gt;
      &lt;/providers&gt;
    &lt;/entityFramework&gt;
  </code></pre>
  
  <p>Again based on tips from @rism I also tried this version (but the GZip error persists):</p>
  
  <pre><code>  &lt;entityFramework&gt;
      &lt;defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"&gt;
        &lt;parameters&gt;
          &lt;parameter value="v12.0" /&gt;
        &lt;/parameters&gt;
      &lt;/defaultConnectionFactory&gt;
      &lt;providers&gt;
        &lt;provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /&gt;
      &lt;/providers&gt;
    &lt;/entityFramework&gt;
  </code></pre>
  
  <p><strong>New update:</strong></p>
  
  <p>I created a brand new Web application with the standard <em>user-security</em> option. I also created an empty database in Azure.</p>
  
  <p>I did nothing but change the default connection string to this:</p>
  
  <pre><code>  &lt;connectionStrings&gt;
      &lt;add name="DefaultConnection" 
           connectionString="data source=mydatabase.database.windows.net;initial catalog=Feedback;persist security info=True;user id=LeaveFeedbackuser;password=mypassword;MultipleActiveResultSets=True;App=EntityFramework"
           providerName="System.Data.SqlClient" /&gt;
    &lt;/connectionStrings&gt;
  </code></pre>
  
  <p>and the default connection factory to this:</p>
  
  <pre><code>  &lt;entityFramework&gt;
      &lt;defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"&gt;
        &lt;parameters&gt;
          &lt;parameter value="v12.0" /&gt;
        &lt;/parameters&gt;
      &lt;/defaultConnectionFactory&gt;
      &lt;providers&gt;
        &lt;provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /&gt;
      &lt;/providers&gt;
    &lt;/entityFramework&gt;
  </code></pre>
  
  <p>On attempting to login I get the following error:</p>
  
  <blockquote>
    <p>The magic number in GZip header is not correct. Make sure you are
    passing in a GZip stream.    Description: An unhandled exception
    occurred during the execution of the current web request. Please
    review the stack trace for more information about the error and where
    it originated in the code. </p>
    
    <p>Exception Details: System.IO.InvalidDataException: The magic number
    in GZip header is not correct. Make sure you are passing in a GZip
    stream.</p>
    
    <p>Source Error: </p>
    
    <p>Line 153:            { Line 154:                var user = new
    ApplicationUser { UserName = model.Email, Email = model.Email }; Line
    155:                var result = await UserManager.CreateAsync(user,
    model.Password); Line 156:                if (result.Succeeded) Line
    157:                {</p>
  </blockquote>