Home > Mobile >  Migration issues after replacing IdentityDbContext with regular DbContext
Migration issues after replacing IdentityDbContext with regular DbContext

Time:07-30

Originally I was using IdentityDbContext for awhile. 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. So I switched my ApplicationDbContext class to inherit DbContext instead.

I cleared the migrations and snapshots, killed and pruned the docker containers. Once I try to do the initial migration I get these errors in the Package Manager Console:

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:

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.

Project.Api
  - Startup.cs
  - Program.cs

Project.App
  - ApplicationDbContext.cs
  - Migrations Folder

Here is how I added the database in 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:

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:

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

As mentioned before, with the DbContextFactory included, it just shows this error:

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.

CodePudding user response:

Ended up being an AWS configuration in the Program.cs file where I wasn't passing my AWS profile to the AWSOptions class.

  • Related