0

我必须扩展 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; }

(我试图覆盖默认UserRolesRoleswith new,但 EF 迁移 throw AmbiguousMatchException)现在我想我必须在应用程序配置中注册我的自定义实现,但我不知道如何。我在Startup.cs

services.AddIdentity<ApplicationUser, ApplicationRole>(/*options*/)

并将超类更改为ApplicationDbContext

public class ApplicationDbContext : IdentityDbContext<ApplicationUser,ApplicationRole,string>

我还应该做什么?或者也许我必须以完全不同的方式解决任务?

4

1 回答 1

1

查看源代码,IdentityDbContext您似乎无法IdentityUserRole在 Identity v3 中使用自己的类。事实上,有一个未解决的问题可以重新添加它。

于 2015-11-18T11:24:24.613 回答