I’m encountering an issue while trying to update my database using Entity Framework Core in my .NET project. When I run the command:
dotnet ef database update --context IdentityDataContext
I receive the following error message:
Build started...
Build succeeded.
Unable to create an object of type 'IdentityDataContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
I have configured my IdentityDataContext
to use PostgreSQL in my DependencyInjection
class as shown below:
public static class DependencyInjection
{
public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration)
{
var connectionString = configuration.GetConnectionString("LoginPostgres");
services.AddDbContext<IdentityDataContext>(options =>
{
options.UseNpgsql(connectionString);
});
services.AddDbContext<LoginContext>(options =>
{
options.UseNpgsql(connectionString);
});
services.AddIdentity<Usuario, IdentityRole>(options =>
{
options.Password.RequireDigit = false;
options.Password.RequiredLength = 4;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
options.Password.RequireLowercase = false;
options.SignIn.RequireConfirmedAccount = false;
options.User.RequireUniqueEmail = true;
}).AddEntityFrameworkStores<IdentityDataContext>().AddDefaultTokenProviders();
services.AddScoped<SignInManager<Usuario>, SignInManager<Usuario>>();
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
services.AddScoped(typeof(IRepositorioGenerico<,>), typeof(RepositorioGenerico<,>));
services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
services.AddScoped<TokenService>();
services.AddScoped<AutenticacaoService>();
services.AddScoped<EmailService>();
services.AddRazorPages();
services.AddHttpContextAccessor();
return services;
}
}
File IdentityDataContext:
using Login.Domain.Entities;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Login.Infrastructure.Data.Context
{
public class IdentityDataContext : IdentityDbContext<Usuario>
{
public IdentityDataContext(DbContextOptions<IdentityDataContext> options) : base(options)
{
}
}
}
Does anyone have an idea of what might be causing this issue? How can I resolve it?
Thank you in advance for your help!
Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create an object of type 'IdentityDataContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
---> System.InvalidOperationException: Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[Login.Infrastructure.Data.Context.IdentityDataContext]' while attempting to activate 'Login.Infrastructure.Data.Context.IdentityDataContext'.
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetServiceOrCreateInstance(IServiceProvider provider, Type type)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass21_4.<FindContextTypes>b__13()
--- End of inner exception stack trace ---
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass21_4.<FindContextTypes>b__13()
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func`1 factory)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String connectionString, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String connectionString, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Unable to create an object of type 'IdentityDataContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
Entity Framework Core cannot resolve the Microsoft.EntityFrameworkCore.DbContextOptions service when trying to activate IdentityDataContext.
5
A workaround can be done to fix that exception, by specifying --project
and --startup-project
options to Entity Framework Core CLI tools like this:
dotnet ef database update --verbose --project **YourProject** --startup-project **YourStartupProject**
-
--project
means which project contains the DbContext class -
--startup-project
means which project contains the database connection information and other information.
Edit
Similar question is answered here.
0
I looked at my own implementation of IdentityDbContext and my constructor just uses a plain DbContextOptions parameter, not a generic variant.
you could try changing this:
namespace Login.Infrastructure.Data.Context
{
public class IdentityDataContext : IdentityDbContext<Usuario>
{
public IdentityDataContext(DbContextOptions<IdentityDataContext> options) : base(options)
{
}
}
}
to this and see if that helps
namespace Login.Infrastructure.Data.Context
{
public class IdentityDataContext : IdentityDbContext<Usuario>
{
public IdentityDataContext(DbContextOptions options) : base(options)
{
}
}
}