ASP.Net 5 + 身份 3 覆盖默认角色实现
ASP.Net 5 + Identity 3 Overriding default Roles implementation
我必须扩展 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 将我的数据存储在相同的默认表中,所以我在 ApplicationDbContext
的 OnModelCreating
方法中编写了以下内容:
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; }
(我试图用 new
覆盖默认 UserRoles
和 Roles
,但 EF 迁移抛出 AmbiguousMatchException
)
现在我想我必须在应用程序配置中注册我的自定义实现,但我不知道如何。我写在 Startup.cs
:
services.AddIdentity<ApplicationUser, ApplicationRole>(/*options*/)
并更改了 ApplicationDbContext
的超类:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser,ApplicationRole,string>
我还应该做什么?或者也许我必须以完全不同的方式解决任务?
正在查看 IdentityDbContext
and it seems you cannot use your own IdentityUserRole
class in Identity v3. In fact, there is an open issue 的源代码以将其添加回去。
我必须扩展 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 将我的数据存储在相同的默认表中,所以我在 ApplicationDbContext
的 OnModelCreating
方法中编写了以下内容:
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; }
(我试图用 new
覆盖默认 UserRoles
和 Roles
,但 EF 迁移抛出 AmbiguousMatchException
)
现在我想我必须在应用程序配置中注册我的自定义实现,但我不知道如何。我写在 Startup.cs
:
services.AddIdentity<ApplicationUser, ApplicationRole>(/*options*/)
并更改了 ApplicationDbContext
的超类:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser,ApplicationRole,string>
我还应该做什么?或者也许我必须以完全不同的方式解决任务?
正在查看 IdentityDbContext
and it seems you cannot use your own IdentityUserRole
class in Identity v3. In fact, there is an open issue 的源代码以将其添加回去。