title: MVC 5: The entity type ApplicationUser is not part of the model for the current context. (DB first Approach)
body: <pre><code>    The entity type ApplicationUser is not part of the model for the current context.
      Line 76:             // This doesn't count login failures towards account lockout
      Line 77:             // To enable password failures to trigger account lockout, change to 
      shouldLockout: true
      Line 78:             var result = await SignInManager.PasswordSignInAsync(model.Email, 
      model.Password, model.RememberMe, shouldLockout: false);
      Line 79:             switch (result)
      Line 80:             {
  </code></pre>
  <p>[InvalidOperationException: The entity type ApplicationUser is not part of the model for the current context.]
  System.Data.Entity.Internal.InternalContext.UpdateEntitySetMappingsForType(Type entityType) +4307818
  System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +38
  System.Data.Entity.Internal.Linq.InternalSet<code>1.Initialize() +54 System.Data.Entity.Internal.Linq.InternalSet</code>1.Include(String path) +18
  System.Data.Entity.Infrastructure.DbQuery<code>1.Include(String path) +68 System.Data.Entity.QueryableExtensions.Include(IQueryable</code>1 source, String path) +135
  System.Data.Entity.QueryableExtensions.Include(IQueryable<code>1 source, Expression</code>1 path) +188
  Microsoft.AspNet.Identity.EntityFramework.UserStore<code>6.GetUserAggregateAsync(Expression</code>1 filter) +260
  Microsoft.AspNet.Identity.EntityFramework.UserStore<code>6.FindByNameAsync(String userName) +536 Microsoft.AspNet.Identity.UserManager</code>2.FindByNameAsync(String userName) +56
  Microsoft.AspNet.Identity.d__4.MoveNext() +376
  System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +102
  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +64
  Microsoft.AspNet.Identity.d__0.MoveNext() +242
  System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +102
  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +64
  Microsoft.AspNet.Identity.d__0.MoveNext() +558
  System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +102
  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +64
  Microsoft.AspNet.Identity.d__d.MoveNext() +550
  System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +102
  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +64
  System.Runtime.CompilerServices.TaskAwaiter<code>1.GetResult() +29 OperationsDashboard.Web.Controllers.&lt;Register&gt;d__15.MoveNext() in E:\2018_project\Smartwires\Operations Dashboard\Development\7-16-2020\OperationsDashboard\OperationsDashboard.Web\Controllers\AccountController.cs:155 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +102 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +64 System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult) +97 System.Web.Mvc.Async.&lt;&gt;c__DisplayClass37.&lt;BeginInvokeAsynchronousActionMethod&gt;b__36(IAsyncResult asyncResult) +17 System.Web.Mvc.Async.WrappedAsyncResult</code>1.CallEndDelegate(IAsyncResult asyncResult) +10
  System.Web.Mvc.Async.WrappedAsyncResultBase<code>1.End() +49 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32 System.Web.Mvc.Async.AsyncInvocationWithFilters.&lt;InvokeActionMethodFilterAsynchronouslyRecursive&gt;b__3d() +50 System.Web.Mvc.Async.&lt;&gt;c__DisplayClass46.&lt;InvokeActionMethodFilterAsynchronouslyRecursive&gt;b__3f() +228 System.Web.Mvc.Async.&lt;&gt;c__DisplayClass33.&lt;BeginInvokeActionMethodWithFilters&gt;b__32(IAsyncResult asyncResult) +10 System.Web.Mvc.Async.WrappedAsyncResult</code>1.CallEndDelegate(IAsyncResult asyncResult) +10
  System.Web.Mvc.Async.WrappedAsyncResultBase<code>1.End() +49 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34 System.Web.Mvc.Async.&lt;&gt;c__DisplayClass2b.&lt;BeginInvokeAction&gt;b__1c() +26 System.Web.Mvc.Async.&lt;&gt;c__DisplayClass21.&lt;BeginInvokeAction&gt;b__1e(IAsyncResult asyncResult) +100 System.Web.Mvc.Async.WrappedAsyncResult</code>1.CallEndDelegate(IAsyncResult asyncResult) +10
  System.Web.Mvc.Async.WrappedAsyncResultBase<code>1.End() +49 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27 System.Web.Mvc.Controller.&lt;BeginExecuteCore&gt;b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13 System.Web.Mvc.Async.WrappedAsyncVoid</code>1.CallEndDelegate(IAsyncResult asyncResult) +29
  System.Web.Mvc.Async.WrappedAsyncResultBase<code>1.End() +49 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +36 System.Web.Mvc.Controller.&lt;BeginExecute&gt;b__15(IAsyncResult asyncResult, Controller controller) +12 System.Web.Mvc.Async.WrappedAsyncVoid</code>1.CallEndDelegate(IAsyncResult asyncResult) +22
  System.Web.Mvc.Async.WrappedAsyncResultBase<code>1.End() +49 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26 System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10 System.Web.Mvc.MvcHandler.&lt;BeginProcessRequest&gt;b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21 System.Web.Mvc.Async.WrappedAsyncVoid</code>1.CallEndDelegate(IAsyncResult asyncResult) +29
  System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
  System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28
  System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
  System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9836613
  System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +50
  System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +163</p>
  <pre><code>
      &lt;add name=&quot;myconnectionstring&quot; connectionString=&quot;metadata=res://*/Models.my_database.csdl|res://*/Models.my_database.ssdl|res://*/Models.my_database.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;data source=localhost;initial catalog=Operations_DB_1;integrated security=False;user id=sa;password=sa123;multipleactiveresultsets=True;application name=EntityFramework&amp;quot;&quot; providerName=&quot;System.Data.EntityClient&quot; /&gt;
  
  
  </code></pre>
  <p>IdentityConfig.cs</p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Data.Entity;
  using System.Linq;
  using System.Security.Claims;
  using System.Threading.Tasks;
  using System.Web;
  using Microsoft.AspNet.Identity;
  using Microsoft.AspNet.Identity.EntityFramework;
  using Microsoft.AspNet.Identity.Owin;
  using Microsoft.Owin;
  using Microsoft.Owin.Security;
  using OperationsDashboard.Web.Models;
  
  namespace OperationsDashboard.Web
  {
      public class EmailService : IIdentityMessageService
      {
          public Task SendAsync(IdentityMessage message)
          {
              // Plug in your email service here to send an email.
              return Task.FromResult(0);
          }
      }
  
      public class SmsService : IIdentityMessageService
      {
          public Task SendAsync(IdentityMessage message)
          {
              // Plug in your SMS service here to send a text message.
              return Task.FromResult(0);
          }
      }
  
      // Configure the application user manager used in this application. UserManager is defined in ASP.NET Identity and is used by the application.
      public class ApplicationUserManager : UserManager&lt;ApplicationUser&gt;
      {
          public ApplicationUserManager(IUserStore&lt;ApplicationUser&gt; store)
              : base(store)
          {
          }
  
          public static ApplicationUserManager Create(IdentityFactoryOptions&lt;ApplicationUserManager&gt; options, IOwinContext context) 
          {
              var manager = new ApplicationUserManager(new UserStore&lt;ApplicationUser&gt;(context.Get&lt;ApplicationDbContext&gt;()));
              // Configure validation logic for usernames
              manager.UserValidator = new UserValidator&lt;ApplicationUser&gt;(manager)
              {
                  AllowOnlyAlphanumericUserNames = false,
                  RequireUniqueEmail = true
              };
  
              // Configure validation logic for passwords
              manager.PasswordValidator = new PasswordValidator
              {
                  RequiredLength = 6,
                  RequireNonLetterOrDigit = true,
                  RequireDigit = true,
                  RequireLowercase = true,
                  RequireUppercase = true,
              };
  
              // Configure user lockout defaults
              manager.UserLockoutEnabledByDefault = true;
              manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
              manager.MaxFailedAccessAttemptsBeforeLockout = 5;
  
              // Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user
              // You can write your own provider and plug it in here.
              manager.RegisterTwoFactorProvider(&quot;Phone Code&quot;, new PhoneNumberTokenProvider&lt;ApplicationUser&gt;
              {
                  MessageFormat = &quot;Your security code is {0}&quot;
              });
              manager.RegisterTwoFactorProvider(&quot;Email Code&quot;, new EmailTokenProvider&lt;ApplicationUser&gt;
              {
                  Subject = &quot;Security Code&quot;,
                  BodyFormat = &quot;Your security code is {0}&quot;
              });
              manager.EmailService = new EmailService();
              manager.SmsService = new SmsService();
              var dataProtectionProvider = options.DataProtectionProvider;
              if (dataProtectionProvider != null)
              {
                  manager.UserTokenProvider = 
                      new DataProtectorTokenProvider&lt;ApplicationUser&gt;(dataProtectionProvider.Create(&quot;ASP.NET Identity&quot;));
              }
              return manager;
          }
      }
  
      // Configure the application sign-in manager which is used in this application.
      public class ApplicationSignInManager : SignInManager&lt;ApplicationUser, string&gt;
      {
          public ApplicationSignInManager(ApplicationUserManager userManager, IAuthenticationManager authenticationManager)
              : base(userManager, authenticationManager)
          {
          }
  
          public override Task&lt;ClaimsIdentity&gt; CreateUserIdentityAsync(ApplicationUser user)
          {
              return user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager);
          }
  
          public static ApplicationSignInManager Create(IdentityFactoryOptions&lt;ApplicationSignInManager&gt; options, IOwinContext context)
          {
              return new ApplicationSignInManager(context.GetUserManager&lt;ApplicationUserManager&gt;(), context.Authentication);
          }
      }
  }
  </code></pre>
  <pre><code>IdentityModels.cs
  </code></pre>
  <pre><code>using System.Data.Entity;
  using System.Security.Claims;
  using System.Threading.Tasks;
  using Microsoft.AspNet.Identity;
  using Microsoft.AspNet.Identity.EntityFramework;
  
  namespace OperationsDashboard.Web.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(&quot;myconnectionstring&quot;, throwIfV1Schema: false)
          {
          }
  
          public static ApplicationDbContext Create()
          {
              return new ApplicationDbContext();
          }
      }
  }
  
  </code></pre>