简体   繁体   English

将 IdentityDbContext 替换为常规 DbContext 后的迁移问题

[英]Migration issues after replacing IdentityDbContext with regular DbContext

Originally I was using IdentityDbContext for awhile.最初我使用 IdentityDbContext 有一段时间了。 Everything was working and I was doing migrations and such.一切正常,我正在进行迁移等。 But recently I decided to let AWS Cognito handle the users and password thus no longer needing IdentityDbContext.但最近我决定让 AWS Cognito 处理用户和密码,因此不再需要 IdentityDbContext。 So I switched my ApplicationDbContext class to inherit DbContext instead.所以我将我的 ApplicationDbContext class 改为继承 DbContext 。

I cleared the migrations and snapshots, killed and pruned the docker containers.我清除了迁移和快照,杀死并修剪了 docker 容器。 Once I try to do the initial migration I get these errors in the Package Manager Console:一旦我尝试进行初始迁移,我会在 Package 管理器控制台中收到以下错误:

An error occurred while accessing the Microsoft.Extensions.Hosting services. 
Continuing without the application service provider. 
Error: Object reference not set to an instance of an object.

Unable to create an object of type 'ApplicationDbContext'. 
For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

After doing some research I added a class that inherits the IDesignTimeDbContextFactory and I was actually able to add migrations but the first error still remained:在做了一些研究之后,我添加了一个继承 IDesignTimeDbContextFactory 的 class 并且我实际上能够添加迁移,但第一个错误仍然存在:

An error occurred while accessing the Microsoft.Extensions.Hosting services. 
Continuing without the application service provider. 
Error: Object reference not set to an instance of an object.

My project structure is setup so the Startup file is in a different project than where the ApplicationDbContext Class is but it's containerized by docker-compose.我的项目结构已设置,因此启动文件与 ApplicationDbContext Class 所在的项目不同,但它由 docker-compose 容器化。

Project.Api
  - Startup.cs
  - Program.cs

Project.App
  - ApplicationDbContext.cs
  - Migrations Folder

Here is how I added the database in Startup.cs:以下是我在 Startup.cs 中添加数据库的方法:

var connectionString = Configuration.GetSection("ConnectionStrings").GetSection("DefaultConnection").Value;
services.AddDbContext<ApplicationDbContext>(options =>
{
  options.UseNpgsql(connectionString,
  b =>
  {
    b.MigrationsAssembly("Project.App");
  });
});

What the ApplicationDbContext.cs looks like: ApplicationDbContext.cs 的样子:

public ApplicationDbContext : DbContext
{
   public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options){}
   // DbSet<Entities>...

   protected override void OnModelCreating(ModelBuilder builder)
   {
     base.OnModelCreating(builder);
     // ...
     // ...
   }
}

public class ApplicationDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
{
   private readonly DbConfig _dbConfig;

   public ApplicationDbContextFactory(IOptionsMonitor<DbConfig> dbConfig)
   {
      _dbConfig = dbConfig.CurrentValue;
   }

   public ApplicationDbContext CreateDbContext(string[] args)
   {
       var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
       optionsBuilder.UseNpgsql(_dbConfig.DefaultConnection);

       return new ApplicationDbContext(optionsBuilder.Options);
   }
}

When I run the migration command I select the Default project as Project.App, and in the Package Manager Console I write:当我运行迁移命令时,我 select 默认项目为 Project.App,在 Package 管理器控制台中我写道:

Add-Migration initMigration -Context ApplicationDbContext -Project Project.App -StartupProject Project.Api.

As mentioned before, with the DbContextFactory included, it just shows this error:如前所述,包含 DbContextFactory 时,它只显示此错误:

An error occurred while accessing the Microsoft.Extensions.Hosting services. 
Continuing without the application service provider. 
Error: Object reference not set to an instance of an object.

Ended up being an AWS configuration in the Program.cs file where I wasn't passing my AWS profile to the AWSOptions class.最终成为 Program.cs 文件中的 AWS 配置,我没有将我的 AWS 配置文件传递给 AWSOptions class。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM