ApplicationUser类型不能在泛型类型或方法'IdentityDbContext <TUser>中用作类型参数'TUser'

Hec*_*res 5 c# asp.net-identity asp.net-core-2.0

尝试在ASP.NET Core 2.0中实现Identity.我遇到很多问题.

Startup.cs

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("Ctrack6_Custom"))
        );


        services.AddIdentity<ApplicationUser, ApplicationRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

etc...
Run Code Online (Sandbox Code Playgroud)

ApplicationUser.cs使用Guid作为密钥.同时设置角色等

// Add profile data for application users by adding properties to the ApplicationUser class
public class ApplicationUser : IdentityUser<Guid>
{
    [MaxLength(50)]
    public string FirstName { get; set; }

    [MaxLength(50)]
    public string LastName { get; set; }

    [MaxLength(5)]
    public string OrgCode { get; set; }

    public ApplicationUser() : base()
    {

    }

}
Run Code Online (Sandbox Code Playgroud)

ApplicationDbContext.cs在类定义中抛出此文件中的错误.ApplicationDbContext抛出此错误:

类型'App.Identity.ApplicationUser'不能在泛型类型或方法'IdentityDbContext'中用作类型参数'TUser'.没有从"App.Identity.ApplicationUser"到"M​​icrosoft.AspNetCore.Identity.IdentityUser"的隐式引用转换.

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        // Customize the ASP.NET Identity model and override the defaults if needed.
        // For example, you can rename the ASP.NET Identity table names and more.
        // Add your customizations after calling base.OnModelCreating(builder);
        builder.Entity<blah>()
        .HasKey(c => new { fields, for, key });

    }

    public DbSet<etc> Etcs {get; set; }

}
Run Code Online (Sandbox Code Playgroud)

Kev*_*vin 9

更改public class ApplicationDbContext : IdentityDbContext<ApplicationUser> 到

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, Guid>
Run Code Online (Sandbox Code Playgroud)

您需要一个类似于ApplicationUser类的ApplicationRole类.根据我的记忆,一旦指定了密钥类型(在本例中为Guid,默认为字符串),即使您没有使用角色,也需要包含角色和密钥类型.