0

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(デフォルトとRoleswithをオーバーライドしようとしましnewたが、EF migration throw AmbiguousMatchException) アプリ構成でカスタム実装を登録する必要があると思いますが、方法がわかりません。私は次のように言いStartup.csます:

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

のスーパークラスを変更しましたApplicationDbContext:

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

他に何をすべきですか?それとも、まったく別のタスクに取り組む必要があるのでしょうか?

4

1 に答える 1

1

ソースを見ると、Identity v3 で独自のクラスをIdentityDbContext使用できないようです。IdentityUserRole実際、これを再度追加するための未解決の問題があります。

于 2015-11-18T11:24:24.613 に答える