C# 类型ApplicationUser不能用作类型参数';TUser';在泛型类型或方法中';IdentityDbContext<;TUser>';
正在尝试在ASP.NET Core 2.0中实现标识。我脑子里有很多问题 Startup.csC# 类型ApplicationUser不能用作类型参数';TUser';在泛型类型或方法中';IdentityDbContext<;TUser>';,c#,asp.net-identity,asp.net-core-2.0,C#,Asp.net Identity,Asp.net Core 2.0,正在尝试在ASP.NET Core 2.0中实现标识。我脑子里有很多问题 Startup.cs public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets call
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...
公共类启动
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
services.AddDbContext(选项=>
options.UseSqlServer(Configuration.GetConnectionString(“Ctrack6_自定义”))
);
服务.额外性()
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();
等
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()
{
}
}
//通过向ApplicationUser类添加属性,为应用程序用户添加配置文件数据
公共类应用程序用户:IdentityUser
{
[MaxLength(50)]
公共字符串名{get;set;}
[MaxLength(50)]
公共字符串LastName{get;set;}
[MaxLength(5)]
公共字符串OrgCode{get;set;}
公共应用程序用户():base()
{
}
}
ApplicationDbContext.cs在类定义的此文件中引发错误。ApplicationDbContext引发此错误:
类型“App.Identity.ApplicationUser”不能用作泛型类型或方法“IdentityDbContext”中的类型参数“TUser”。没有从“App.Identity.ApplicationUser”到“Microsoft.AspNetCore.Identity.IdentityUser”的隐式引用转换
public类ApplicationDbContext:IdentityDbContext
{
公共应用程序DBContext(DbContextOptions选项)
:基本(选项)
{
}
模型创建时受保护的覆盖无效(ModelBuilder)
{
基于模型创建(生成器);
//自定义ASP.NET标识模型,并根据需要覆盖默认值。
//例如,可以重命名ASP.NET标识表名称等。
//在调用base.OnModelCreating(builder)后添加自定义项;
builder.Entity()
.HasKey(c=>new{fields,for,key});
}
公共数据库集Etcs{get;set;}
}
更改公共类应用程序bContext:IdentityDbContext
到
public类ApplicationDbContext:IdentityDbContext
您需要有一个类似于ApplicationUser类的ApplicationRole类即使不使用角色,也需要包含角色和密钥类型。我不知道如何从该错误中获得该解决方案:/非常感谢:)从IdentityContext继承时,我们必须按预期顺序保留所有参数。这意味着您无法更改其位置!因此,对于网络核心,我们必须继承按所需抽象类中的定义键入并保留参数
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, ApplicationRole, Guid>