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
メソッドに記述しました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 migration throw AmbiguousMatchException
) アプリ構成でカスタム実装を登録する必要があると思いますが、方法がわかりません。私は次のように言いStartup.cs
ます:
services.AddIdentity<ApplicationUser, ApplicationRole>(/*options*/)
のスーパークラスを変更しましたApplicationDbContext
:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser,ApplicationRole,string>
他に何をすべきですか?それとも、まったく別のタスクに取り組む必要があるのでしょうか?