gpt4 book ai didi

c# - 类型 ApplicationUser 不能用作泛型类型或方法 'TUser' 中的类型参数 'IdentityDbContext'

转载 作者:行者123 更新时间:2023-12-01 19:44:16 25 4
gpt4 key购买 nike

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

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 作为 key 。还可以在Role等中设置

// 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 抛出此错误:

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 类。据我记得,一旦您指定了 key 类型(在本例中为 Guid,默认为字符串),即使您不使用角色,您也需要包含角色和 key 类型。

关于c# - 类型 ApplicationUser 不能用作泛型类型或方法 'TUser' 中的类型参数 'IdentityDbContext<TUser>',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49788816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com