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

后端 未结 6 1516
鱼传尺愫
鱼传尺愫 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:03

    I'll explain here with the code exampels :).

    The trick is, they are already in the IdentityDbContext (AspNetRoles, AspNetUserClaims, AspNetUsers, ....)

    In the IdentityModel you will see ApplicationUser is empty at the top. If you want to customize these users or roles, just add properties here and then update your database via the console

    Example of my context

    public class ApplicationDbContext : IdentityDbContext
    {
        public ApplicationDbContext()
            : base("DefaultConnection")
        {
        }
    
        public DbSet Requests { get; set; }
        public DbSet Reservations { get; set; }
        public DbSet PriceTypes { get; set; }
        public DbSet Products { get; set; }
        public DbSet Prices { get; set; }
        public DbSet Posts { get; set; }
        public DbSet Counts { get; set; }
        public DbSet Invoices { get; set; }
        public DbSet InvoiceLines { get; set; }
    
        ...
    
    }
    

    So no application user is defined here, but I did add more properties to it, example:

    public class ApplicationUser : IdentityUser
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string GroupName { get; set; }
        public string Email { get; set; }
        [StringLength(15)]
        public string Phone { get; set; }
        public string Remark { get; set; }
        public DateTime? BirthDate { get; set; }
        public DateTime ValidFrom { get; set; }
        public DateTime ValidUntil { get; set; }
    
        public string Street { get; set; }
        public string ZipCode { get; set; }
        public string City { get; set; }
    
        public virtual ICollection Requests { get; set; } 
    }
    

提交回复
热议问题