21

整数キーを使用するように ASP.NET Identity 3 をカスタマイズしようとしています。

public class ApplicationUserLogin : IdentityUserLogin<int> { }
public class ApplicationUserRole : IdentityUserRole<int> { }
public class ApplicationUserClaim : IdentityUserClaim<int> { }

public sealed class ApplicationRole : IdentityRole<int>
{
  public ApplicationRole() { }
  public ApplicationRole(string name) { Name = name; }
}

public class ApplicationUserStore : UserStore<ApplicationUser, ApplicationRole, ApplicationDbContext, int>
{
  public ApplicationUserStore(ApplicationDbContext context) : base(context) { }
}

public class ApplicationRoleStore : RoleStore<ApplicationRole, ApplicationDbContext, int>
{
  public ApplicationRoleStore(ApplicationDbContext context) : base(context) { }
}

public class ApplicationUser : IdentityUser<int>
{
}

public sealed class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, int>
{
  private static bool _created;

  public ApplicationDbContext()
  {
    // Create the database and schema if it doesn't exist
    if (!_created) {
      Database.AsRelational().Create();
      Database.AsRelational().CreateTables();
      _created = true;
    }
  }
}

これは問題なくコンパイルされますが、実行時エラーがスローされます。

System.TypeLoadException

GenericArguments[0]、'TeacherPlanner.Models.ApplicationUser'、'Microsoft.AspNet.Identity.EntityFramework.UserStore`4[TUser,TRole,TContext,TKey]' は、型パラメーター 'TUser' の制約に違反しています。

の署名UserStoreは次のとおりです。

public class UserStore<TUser, TRole, TContext, TKey>
where TUser : Microsoft.AspNet.Identity.EntityFramework.IdentityUser<TKey>
where TRole : Microsoft.AspNet.Identity.EntityFramework.IdentityRole<TKey>
where TContext : Microsoft.Data.Entity.DbContext
where TKey : System.IEquatable<TKey>

ApplicationUser正確にはIdentityUser<int>です。これは探しているものではありませんか?

4

3 に答える 3

9

この問題にも遭遇しました。同じエラーが発生していたため、 IdentityRoleキー タイプも追加する必要がありました。

        services.AddIdentity<ApplicationUser, IdentityRole<int>>()
            .AddEntityFrameworkStores<ApplicationDbContext,int>()
            .AddDefaultTokenProviders();
于 2016-01-09T20:13:43.687 に答える
2

EF Core ユーザーへの注意事項

上記に加えて、.Netコア3.0を使用している場合(以前のバージョンについては不明)、AddEntityFrameworkStores<TContext,TKey>メソッドはなくなりました。

代わりに、の汎用バリアントがあるIdentityDbContextため、代わりに DbContext を派生させますIdentityDbContext<TUser,TRole,TKey>

例えば私の場合

class ApplicationUser : IdentityUser<int> {...}
class ApplicationDbContext : IdentityDbContext<ApplicationUser, IdentityRole<int>, int> {...}

その後、起動時に使用できますservices.AddDefaultIdentity<ApplicationUser>

https://github.com/aspnet/Identity/issues/1082の最後のコメントから引用

于 2019-11-02T18:15:22.407 に答える