Home » Ado.Net » Entity Framework » The entity type ApplicationUser is not part of the model for the current context.

The entity type ApplicationUser is not part of the model for the current context.

If you are using Asp.net Identity (OWIN authentification framework) with a custom user schema you may stumble into an error that you model is not part of the context.

The entity type ApplicationUser is not part of the model for the current context.

NotPartOfTheModelOfTheCurrentContext

This exception is raised if you go to the login screen and try to log in. It occurs if you have a DbContext class that inherit directly to DbContext and not of IdentityDbContext.

Your database context must be aware of Identity tables.

public class DatabaseContext :IdentityDbContext<ApplicationUser>

Having this inheritance solve the problem.

If you like my article, think to buy my annual book, professionally edited by a proofreader. directly from me or on Amazon. I also wrote a TypeScript book called Holistic TypeScript

28 Responses so far.

  1. Rich says:

    Is there any other reason that this might happen? My code is exactly as you are suggesting but I still get this error.

    • Hi Rich, have you rebuild your database with Entity Framework after the change of your Database Context class? Can you post more detail about the error you are getting and how is your configuration.

  2. Dennis says:

    Hi Patrick, I’m still having the same issues as @Rich. I have completely rebuilt my solution to no avail (Note that I am running of an Azure Web db for this…don’t know if that would make any difference or not). Let me know if you have any further insights. – Thanks


    public class ApplicationUser : IdentityUser
    {
    }

    public class ApplicationDbContext : IdentityDbContext
    {
    public ApplicationDbContext()
    : base("MyContext")
    {
    }
    }

  3. Matt says:

    I was having this same problem, and I recall having a similar problem working with SimpleMembership in MVC4.

    I’m doing database first development, so I have an EDMX file. Turns out, ASP.NET Identity does not like the connection string that is created when you generate your .edmx model file. If you are using a. EDM connection string in :base(“EDMConnString”) you will most likely have this problem.

    I fixed this by creating a standard connection string that pointed to the database where the ASP.NET Identity tables are (in my case the same database), used that connection string in :base, and it worked.

    • Chris says:

      Matt, your comment was exactly my same problem. I have an ASP.NET MVC 5 project with WebApi 2.1. I wanted to use the ApplicationUser object created by the template, but when I made the changes, I got this error. By simply adding a second (normal) connection string that was not the complicated one generated when you add an EDMX file to your project, then the app worked. Thanks!

    • Andriy says:

      Thanks a lot))

    • Mathias says:

      Thank you a thousand times thank you.

    • Hari says:

      I’m using modal first method. I’m still getting the error. Any suggestions ?

    • Mitch says:

      Thanks Matt!

    • Karu says:

      Thanks Matt. Your answer solved my problem.

    • Denys says:

      Thanks, Matt.
      It helped me a lot!

    • pss688 says:

      Thank you very much Matt

    • Seena A. says:

      Thanks man, solved my issue.

      • Ingo says:

        Would you mind sharing your connection strings? When I use a “normal” connection string, I get an UnintentionalCodeFirstException thrown in OnModelCreating()) saying that this would activate model first and I had to use an Entity Framework connection string.

    • Burak says:

      I have lots hours to dealing with this. It works.

      Thanks a lot.

    • Imran says:

      Hi!

      I cannot access the ASPNetUsers data once Web App is deployed on Azure.

      Web apps on my local dev machine and on Azure Server, both are pointing to the Azure SQL Database. But the users that are created from the deployed application is not accessible from the local app and vice versa. I can Register users and login from the deployed site but the users created on the site deployed on Azure does not appear in the ASPNetUsers table.

      How is this possible if the connection string is pointing to the same database?

      Both my connection strings are as follows:

      Please help if anyone knows the answer.

      Thanks,

  4. Josef says:

    Hi,
    i still have the same problem.
    I’m trying to create a new mvc project, to modify the Application User Class with some custom properties. Then i add a new Ado.Net connection to my external Sql server 2012 DB, and i modify The IdentityContextDB as is:
    public class MyDbContext : IdentityDbContext
    {
    public MyDbContext()
    : base(“ETAEntita”)
    {

    }
    }

    then when i run the solution, and try to register a new user i got the same error…The entity type ApplicationUser is not part of the model for the current context.

    Anyone can Help me?? No one with my same Problem?

    Thanks a lot

  5. JSK says:

    Hey Thanks Matt, I’ve been having problems with this for a while and your answer was exactly what i needed to get the user creation template to work on my own database.

  6. Sri says:

    Thank you so much, finally got it!

  7. Nico says:

    It seems to me like you have to have 2 connection strings in web.config. One is for identity login and the other one is for other tables in the SAME database. This is ridiculous. There should be one connectionstring in web.config that works for them all since all these tables are from the same database.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.