无法在 EF Core 上创建数据库迁移,但当您要求 EnsureCreate 数据库时可以

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

我正在尝试向服务注入一些存储库,向控制器注入一些服务,但我使用的 DbContext 是一个不是启动项目的程序集,但奇怪的是,当我尝试调用创建迁移时,它告诉我这种设计是不允许的,但如果我调用 EnsureCreated() 方法,它允许我创建数据库......可能会发生什么

错误 Microsoft.EntityFrameworkCore.Design.OperationException: 无法创建 'AppDbContext' 类型的对象。有关设计时支持的不同模式,请参阅 https://go.microsoft.com/fwlink/?linkid=851728 ---> System.InvalidOperationException: 无法解析类型 '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 工厂的服务) 在 Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(字符串上下文类型) 在 Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(字符串名称、字符串输出目录、字符串上下文类型、字符串命名空间) 在 Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl

这里是服务 DI

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();
        }

尝试在 DbContext 中添加无参数构造函数 class。

如果这不起作用,您可能需要按照 here 所述添加设计时工厂。