I am using NET Core 3.1 + EntityFramework + PSQL. Trying to follow tutorials and msdn but my code doesnt work.
Here`s my startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
//params
};
});
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<MyContext>()
}
Here`s my MyContext.cs
public class MyContext : IdentityDbContext<ApplicationUser>
{
public DbSet<Flowers> Flowers { get; set; }
public DbSet<Post> Posts { get; set; }
public MyContext() : base()
{
Database.EnsureCreated();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql("Host=localhost...");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
Here`s my ApplicationUser.cs
public class ApplicationUser : IdentityUser
{
public string SpecialNumber{ get; set; }
public ICollection<Flower> Flowers { get; set; }
}
When i try to run app i got an exception System.AggregateException: 'Some services are not able to be constructed'
Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.ISecurityStampValidator Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.SecurityStampValidator
1[FlowersApp.Models.ApplicationUser]': Unable to resolve service for type 'FlowersApp.Models.MyContext' while attempting to activate 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore
9[FlowersApp.Models.ApplicationUser,Microsoft.AspNetCore.Identity.IdentityRole,FlowersApp.Models.MyContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim1[System.String],Microsoft.AspNetCore.Identity.IdentityUserRole
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken
1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim1[System.String]]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.ITwoFactorSecurityStampValidator Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.TwoFactorSecurityStampValidator
1[System.String]]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.ITwoFactorSecurityStampValidator Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.TwoFactorSecurityStampValidator
1[FlowersApp.Models.ApplicationUser]': Unable to resolve service for type 'FlowersApp.Models.MyContext' while attempting to activate 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore9[FlowersApp.Models.ApplicationUser,Microsoft.AspNetCore.Identity.IdentityRole,FlowersApp.Models.MyContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserRole1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim
1[System.String]]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.IUserClaimsPrincipalFactory1[FlowersApp.Models.ApplicationUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserClaimsPrincipalFactory
1[FlowersApp.Models.ApplicationUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserClaimsPrincipalFactory
2[FlowersApp.Models.ApplicationUser,Microsoft.AspNetCore.Identity.IdentityRole]': Unable to resolve service for type 'FlowersApp.Models.MyContext' while attempting to activate 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore9[FlowersApp.Models.ApplicationUser,Microsoft.AspNetCore.Identity.IdentityRole,FlowersApp.Models.MyContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserRole1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim
1[System.String]]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.UserManager1[FlowersApp.Models.ApplicationUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserManager
1[FlowersApp.Models.ApplicationUser]': Unable to resolve service for type 'FlowersApp. Models.MyContext' while attempting to activate 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore9[FlowersApp.Models.ApplicationUser,Microsoft.AspNetCore.Identity.IdentityRole,FlowersApp.Models.MyContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserRole1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim
1[System.String]]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.SignInManager1[FlowersApp.Models.ApplicationUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.SignInManager
1[FlowersApp.Models.ApplicationUser]': Unable to resolve service for type 'FlowersApp.Models.MyContext' while attempting to activate 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore9[FlowersApp.Models.ApplicationUser,Microsoft.AspNetCore.Identity.IdentityRole,FlowersApp.Models.MyContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim
9[FlowersApp.Models.ApplicationUser,Microsoft.AspNetCore.Identity.IdentityRole,FlowersApp.Models.MyContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserRole1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim
1[System.String]]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.RoleManager1[Microsoft.AspNetCore.Identity.IdentityRole] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.RoleManager
1[Microsoft.AspNetCore.Identity.IdentityRole]': Unable to resolve service for type 'FlowersApp.Models.MyContext' while attempting to activate 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.RoleStore5[Microsoft.AspNetCore.Identity.IdentityRole,FlowersApp.Models.MyContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserRole
1[System.Str ing],Microsoft.AspNetCore.Identity.IdentityRoleClaim1[System.String]]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.IUserStore
1[FlowersApp.Models.ApplicationUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore9[FlowersApp.Models.ApplicationUser,Microsoft.AspNetCore.Identity.IdentityRole,FlowersApp.Models.MyContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserRole1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim
1[System.String]]': Unable to resolve service for type 'FlowersApp.Models.MyContext' while attempting to activate 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore9[FlowersApp.Models.ApplicationUser,Microsoft.AspNetCore.Identity.IdentityRole,FlowersApp.Models.MyContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim
9[FlowersApp.Models.ApplicationUser,Microsoft.AspNetCore.Identity.IdentityRole,FlowersApp.Models.MyContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserClaim
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserRole1[System.String],Microsoft.AspNetCore.Identity.IdentityUserLogin
1[System.String],Microsoft.AspNetCore.Identity.IdentityUserToken1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim
1[System.String]]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.IRoleStore1[Microsoft.AspNetCore.Identity.IdentityRole] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.EntityFrameworkCore.RoleStore
5[Microsoft.AspNetCore.Identity.IdentityRole,FlowersApp.Models.MyContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserRole1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim
1[System.String]]': Unable to resolve service for type 'FlowersApp.Models.MyContext' while attempting to activate 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.RoleStore5[Microsoft.AspNetCore.Identity.IdentityRole,FlowersApp.Models.MyContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserRole
5[Microsoft.AspNetCore.Identity.IdentityRole,FlowersApp.Models.MyContext,System.String,Microsoft.AspNetCore.Identity.IdentityUserRole
1[System.String],Microsoft.AspNetCore.Identity.IdentityRoleClaim`1[System.String]]'.)
Cant find reason of this error. Are my clases inherited right?
App runs when i delete this part of code from Startup.cs
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<MyContext>()
将此添加到您的ConfigureServices
的顶部您需要告诉框架它如何创建MyContext
的实例(注入该服务)
services.AddDbContext<MyContext>(options => /*<<<ADD YOUR DBCONTEXT CONFIGURATION >>>*/);
You forgot to write this line in your ConfigureServices
section
services.AddDbContext<MyContext>(options =>
options.UseNpgsql("your connection string"));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.