I have a solution that contains three project.
EmployeeManagement.Database
-- class library where the DbContext
is definedEmployeeManagement.Entities
-- class library that contains all my entitiesEmployeeManagement.WebAPI
-- Web API projectI am trying to create a DbContext
that inherits from IdentityDbContext
, but I got this error when I try to use dotnet ef migrations add InitialMigration
from my EmployeeManagement.WebAPI
project.
ERROR:
An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: 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[EmployeeManagement.Entities.ApplicationUser]': Unable to resolve service for type 'System.TimeProvider' while attempting to activate 'Microsoft.AspNetCore.Identity.IdentityBuilderExtensions+PostConfigureSecurityStampValidatorOptions'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.ITwoFactorSecurityStampValidator Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.TwoFactorSecurityStampValidator
1[EmployeeManagement.Entities.ApplicationUser]': Unable to resolve service for type 'System.TimeProvider' while attempting to activate 'Microsoft.AspNetCore.Identity.IdentityBuilderExtensions+PostConfigureSecurityStampValidatorOptions'.) (Error while validating the service descriptor 'ServiceType: Microsoft.Extensions.Options.IPostConfigureOptions1[Microsoft.AspNetCore.Identity.SecurityStampValidatorOptions] Lifetime: Singleton ImplementationType: Microsoft.AspNetCore.Identity.IdentityBuilderExtensions+PostConfigureSecurityStampValidatorOptions': Unable to resolve service for type 'System.TimeProvider' while attempting to activate 'Microsoft.AspNetCore.Identity.IdentityBuilderExtensions+PostConfigureSecurityStampValidatorOptions'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.DataProtectorTokenProvider
1[EmployeeManagement.Entities.ApplicationUser] Lifetime: Transient ImplementationType: Microsoft.AspNetCore.Identity.DataProtectorTokenProvider1[EmployeeManagement.Entities.ApplicationUser]': Unable to resolve service for type 'Microsoft.AspNetCore.DataProtection.IDataProtectionProvider' while attempting to activate 'Microsoft.AspNetCore.Identity.DataProtectorTokenProvider
1[EmployeeManagement.Entities.ApplicationUser]'.)No DbContext was found in assembly 'EmployeeManagement.WebAPI'. Ensure that you're using the correct assembly and that the type is neither abstract nor generic.
My DbContext
:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options): base(options)
{
}
public DbSet<ApplicationUser> ApplicationUsers { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
}
}
ApplicationUser
class:
public class ApplicationUser : IdentityUser
{
}
program.cs
:
using EmployeeManagement.Database;
using EmployeeManagement.Entities;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
builder.Services.AddAuthorization();
builder.Services.AddIdentityCore<ApplicationUser>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddApiEndpoints();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.MapIdentityApi<ApplicationUser>();
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
appsettings.json
:
{
"ConnectionStrings": {
"DefaultConnection": "Server=localhost;Database=EmployeeManagementDb;Persist Security Info=True;User ID=PPPP;Password=PPPPPP;MultipleActiveResultSets=True;"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
For me, I had a solution with following folder structure.
soln
--> Data
--> Service
--> Model
To run migration in Data folder whereas startup class is set to Service, please follow below Package Manager Console Commands to run one-by-one.
Add Migration Command
Add-Migration {Migration_Name} -Project Data -StartupProject Service
NOTE: Replace {Migration_Name} with new Migration file Name
Update Database Command
Update-Database -Project Data -StartupProject Service
Once all required packages installed and set your WebAPI project as startup project, this should work.