ASP.Net 5 + Identity 3覆盖默认角色实现

时间:2015-11-18 10:09:09

标签: c# asp.net-core asp.net-identity-3

我必须在Identity 3中扩展Roles的默认实现。所以我编写了子类:

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的{​​{1}}方法中编写了以下内容:

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; } 覆盖默认UserRolesRoles,但EF迁移抛出new) 现在我想我必须在应用程序配置中注册我的自定义实现,但我不知道如何。我在AmbiguousMatchException

Startup.cs

并更改了services.AddIdentity<ApplicationUser, ApplicationRole>(/*options*/) 的超类:

ApplicationDbContext

我还应该怎么办?或许我必须完全解决这个任务?

1 个答案:

答案 0 :(得分:1)

查看IdentityDbContext的来源,似乎您无法在Identity v3中使用自己的IdentityUserRole类。事实上,有一个open issue可以将其添加回来。

相关问题