Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cannot create database migration on EF Core but It can when you ask to EnsureCreate database

I am trying to inject some repository to a service and some services to a controller but I am using the DbContext is an assembly that is not the startup project but oddily when i try to call to create a migration it tells me this design is not allowed but if I call the EnsureCreated() method it allows me to create the database… what could be happening

the error
Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create an object of type ‘AppDbContext’. 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.DbContextOptions1[Repository.AppDbContext.AppDbContext]' while attempting to activate 'Repository.AppDbContext.AppDbContext'. 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__DisplayClass13_4.<FindContextTypes>b__13() --- End of inner exception stack trace --- at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass13_4.<FindContextTypes>b__13() at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func1 factory)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl

here is the Service DI

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContextPool<AppDbContext>(options =>
            {
                options.UseSqlServer(Configuration.GetConnectionString("ManagementServiceDb"));
            });
            services.AddIdentity<ApplicationUser, IdentityRole>(options => { 
                options.SignIn.RequireConfirmedAccount = false;
                options.User.RequireUniqueEmail = true;
            }).AddEntityFrameworkStores<AppDbContext>();
            services.AddScoped<IRepository<Producto>, ProductoRepositorio>();
            services.AddScoped<IRepository<RegistroActividad>, RegistroActividadRepositorio>();
            services.AddScoped<IRepository<ProductoRegistroActividad>, RegistroActividadesProductoRepositorio>();
            services.AddScoped<IRepository<TipoActividad>, TipoActividadRepositorio>();
            services.AddScoped<IRepository<ApplicationUser>, UsuarioRepositorio>();

            services.AddScoped<UnidadRepositorio>();

            services.AddScoped<ProductosServicios>();
            services.AddScoped<UsuarioServicio>();

            services.AddScoped<UnidadServicios>();

            services.AddControllersWithViews();
        }

>Solution :

Try to add a parameterless constructor in your DbContext class.

If that doesn’t work you might need to add a design time factory as described here.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading