我必须扩展 Identity 3 中角色的默认实现。所以我编写了子类:
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; }
}
然后,由于我希望 Entity Framework 7 将我的数据存储在相同的默认表中,我在OnModelCreating
方法中编写了以下内容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");
我还定义了以下属性ApplicationDbContext
:
public DbSet<ApplicationUserRole> MyUserRoles { get; set; }
public DbSet<ApplicationRole> MyRoles { get; set; }
(我试图覆盖默认UserRoles
和Roles
with new
,但 EF 迁移 throw AmbiguousMatchException
)现在我想我必须在应用程序配置中注册我的自定义实现,但我不知道如何。我在Startup.cs
:
services.AddIdentity<ApplicationUser, ApplicationRole>(/*options*/)
并将超类更改为ApplicationDbContext
:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser,ApplicationRole,string>
我还应该做什么?或者也许我必须以完全不同的方式解决任务?