在通用类型或方法"IdentityDbContext< TUser>"中,类型ApplicationUser的类型不能用作类型参数"TUser". [英] The type ApplicationUser cannot be used as type parameter 'TUser' in the generic type or method 'IdentityDbContext<TUser>'

查看:125
本文介绍了在通用类型或方法"IdentityDbContext< TUser>"中,类型ApplicationUser的类型不能用作类型参数"TUser".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在ASP.NET Core 2.0中实现身份.在解决这个问题时遇到很多问题.

Trying to implement Identity in ASP.NET Core 2.0. Having many problems getting my head around this.

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...

ApplicationUser.cs 使用Guid作为密钥.还要在角色等中设置

ApplicationUser.cs Using Guid for key. Also set in Role, etc

// 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()
    {

    }

}

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

ApplicationDbContext.cs Error is thrown in this file on the class definition. ApplicationDbContext is throwing this error:

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

The type 'App.Identity.ApplicationUser' cannot be used as type parameter 'TUser' in the generic type or method 'IdentityDbContext'. There is no implicit reference conversion from 'App.Identity.ApplicationUser' to 'Microsoft.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; }

}

推荐答案

更改public class ApplicationDbContext : IdentityDbContext<ApplicationUser>

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

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

You will need to have an ApplicationRole class similar to you ApplicationUser class. From what I remember once you specify the key type (in this case Guid, the default being string) you need to include the role and the key type even if you are not using roles.

这篇关于在通用类型或方法"IdentityDbContext&lt; TUser&gt;"中,类型ApplicationUser的类型不能用作类型参数"TUser".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆