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

前端 未结 4 1392
轻奢々
轻奢々 2021-01-01 21:56

I am seeing the same issue as this question, but the scenario presented there doesnt seem to apply so I think I have a different issue. In fact, I\'m seeing several question

相关标签:
4条回答
  • 2021-01-01 22:37

    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.

    Something like this

    <add name="IdentityConnection" connectionString="data source=THEJUS\SQLSERVER2014;initial catalog=IdentitySample;integrated security=True;MultipleActiveResultSets=True;App=IdentitySample.Admin" providerName="System.Data.SqlClient" />
    
    0 讨论(0)
  • 2021-01-01 22:42

    When you are using a custom user class with ASP.NET Identity, you have to make sure that you explicitly specify the custom user class type <T> to both the UserManager and the UserStore on instantiation.

    private UserManager<UserModel> _userManager;
    
    public AccountController()
    {
        AuthContext _ctx = new AuthContext();
    
        UserStore<UserModel> userStore = new UserStore<UserModel>(_ctx);
        _userManager = new UserManager<UserModel>(userStore);     
    }
    

    or in shorter form (like your reply comment):

    private UserManager<UserModel> _userManager;
    
    public AccountController()
    {
        AuthContext _ctx = new AuthContext();    
        _userManager = new UserManager<UserModel>(new UserStore<UserModel>(_ctx));     
    }
    

    If the type is allowed to defaulted to IdentityUser when you want to use a custom class you will experience the error you reported.

    0 讨论(0)
  • 2021-01-01 22:42

    Here is some step i figured out while resolving this issue

    1. First Check your database for Table of ASP.Net Identity
    2. Create these table on your database if not exist you can also apply migration
    3. Check the below image and verify your code

    Register Action

    IdentityDbContext Class

    0 讨论(0)
  • I got this error when I introduced DI to my project. Using AutoFac and Identity I had to add the following: builder.RegisterType<ApplicationDbContext>().As<DbContext>().InstancePerLifetimeScope();

    Without this, when AutoFac was creating my UserStore instance, it was using the default UserStore() ctor which creates a new IdentityDbContext, not my ApplicationDbContext.

    With this line, UserStore(DbContext context) ctor gets called, with my ApplicationDbContext.

    0 讨论(0)
提交回复
热议问题