I have to extend default implementation of Roles in Identity 3. So I wrote subclasses:
public class ApplicationRole:IdentityRole
{
public string Description { get; set; }
public DateTime CreationDate { get; set; }
}
public class ApplicationUserRole:IdentityUserRole<string>
{
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
Then, since I want Entity Framework 7 to store my data in the same default tables I wrote the following in OnModelCreating
method of ApplicationDbContext
:
builder.Model.RemoveEntityType(new Microsoft.Data.Entity.Metadata.EntityType(typeof(IdentityRole), builder.Model));
builder.Model.RemoveEntityType(new Microsoft.Data.Entity.Metadata.EntityType(typeof(IdentityUserRole<string>),builder.Model));
builder.Entity<ApplicationRole>().ToTable("AspNetRoles");
builder.Entity<ApplicationUserRole>().HasKey(r => new { UserId = r.UserId, RoleId = r.RoleId });
builder.Entity<ApplicationUserRole>().ToTable("AspNetUserRoles");
Also I defined properties in ApplicationDbContext
:
public DbSet<ApplicationUserRole> MyUserRoles { get; set; }
public DbSet<ApplicationRole> MyRoles { get; set; }
(I tried to override default UserRoles
and Roles
with new
, but EF migration throw AmbiguousMatchException
)
Now I suppose I have to register my custom implementation in app configuration, but I have no idea how. I whote in Startup.cs
:
services.AddIdentity<ApplicationUser, ApplicationRole>(/*options*/)
and changed the superclass for ApplicationDbContext
:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser,ApplicationRole,string>
What else should I do? Or maybe I must address the task completely different?