Extending ASP.NET Identity Roles: IdentityRole is not part of the model for the current context

后端 未结 6 1525
鱼传尺愫
鱼传尺愫 2020-12-15 08:37

I\'m trying to use the new ASP.NET Identity in my MVC5 application, specifically I\'m trying to integrate ASP.NET Identity into an existing database. I\'ve already read the

6条回答
  •  别那么骄傲
    2020-12-15 09:01

    You have to specify during the creation of User Store that AspNetRole is used instead of IdentityRole. You can achieve this by using the UserStore class with 6 type parameters:

    new UserStore(new PayrollDBEntities());
    

    This indicates changes at User Manager creation as well. Here is a simplified example about the creation of needed instances:

    public class AspNetUser : IdentityUser { /*customization*/ }
    
    public class AspNetRole : IdentityRole { /*customization*/ }
    
    public class PayrollDBEntities : IdentityDbContext //or : IdentityDbContext  
    {
    }
    
    public class Factory 
    {
        public IdentityDbContext DbContext 
        { 
            get 
            {
                return new PayrollDBEntities();
            } 
        }
    
        public UserStore UserStore
        {
            get 
            {                
                return new UserStore(DbContext);
            }
        }
    
        public UserManager UserManager
        { 
            get 
            {
                return new UserManager(UserStore);
            } 
        }
    
        public RoleStore RoleStore 
        {
            get 
            {
                return new RoleStore(DbContext);
            }
        }
    
        public RoleManager RoleManager 
        {
            get 
            {
                return new RoleManager(RoleStore);
            }
        }
    }
    

提交回复
热议问题