0

Similar questions are:

More than one DbContext was found - answers mainly discuss syntax

Unable to create an object of type 'ApplicationDbContext'. For the different patterns supported at design time - nothing here

After a year away from this project I decided to change some database fields. So I have now remodelled the database schema for my ASP .Net Entity Frameworks (with Identity) project. My changes have remodelled my data table NOT the identity part of my project.

Get-Package
Id                                  Versions                                 ProjectName
--                                  --------                                 -----------
bootstrap                           {5.1.0}                                  rollbase   
Microsoft.EntityFrameworkCore.Sq... {5.0.12}                                 rollbase   
Microsoft.EntityFrameworkCore.De... {5.0.11}                                 rollbase   
Microsoft.AspNetCore.Diagnostics... {5.0.9}                                  rollbase   
Microsoft.AspNetCore.Identity.En... {5.0.9}                                  rollbase   
Microsoft.EntityFrameworkCore.Tools {5.0.11}                                 rollbase   
Microsoft.VisualStudio.Web.CodeG... {5.0.2}                                  rollbase   
Microsoft.AspNetCore.Identity.UI    {5.0.12}                                 rollbase

I decided to remove my migrations folder, to begin again, because I already had a lot of migrations. I checked on what database contexts I have:

PM> Get-DbContext
Build started...
Build succeeded.
The Entity Framework tools version '5.0.11' is older than that of the runtime '5.0.12'. Update the tools for the latest features and bug fixes.

ApplicationDbContext                             
rollbase.Areas.Identity.Data.ApplicationDbContext:

For:

PM> Add-Migration InitialCreate -Verbose

I get this familiar error:

More than one DbContext was found. Specify which one to use. etc

Using the output of Get-DbContext I try:

PM> Add-Migration InitialCreate -Context ApplicationDbContext

Even though I specify the context I still get the 'More than one DbContext was found' Using:

PM> Add-Migration InitialCreate -Context IdentityDbContext -Verbose

I get: No DbContext named 'IdentityDbContext' was found.

Recapping on what I know about my database context:

The database context is registered with the Dependency Injection container in the ConfigureServices method in Startup.cs file:

services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(
                   Configuration.GetConnectionString("DefaultConnection")));

Here. Just like the output from Get-DbContext It is clear that the DbContext to be used is ApplicationDbContext

However. After reading This DbContext Lifetime, Configuration, and Initialization the following caught my attention:

"The ApplicationDbContext class must expose a public constructor with a DbContextOptions<ApplicationDbContext> parameter. This is how context configuration from AddDbContext is passed to the DbContext. For example:"

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }
}

But my ApplicationDbContext class looks like this:

public class ApplicationDbContext : IdentityDbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }

So my ApplicationDbContext inherits from IdentityDbContext instead of DbContext? I found this old stackoverflow post ASP.NET Identity DbContext confusion which discusses the same problem. I must confess I got lost reading the proposals for dealing with this issue. Partly due to the fact that I have a solution that I've had running fine. Deployed to Azure etc.

The last migration shows on GitHub repo like this:

namespace rollbase.Data.Migrations
{
    [DbContext(typeof(ApplicationDbContext))]
    [Migration("20211015203803_UpdateIncludeEntryTable")]

At this point I'm confused. I had previously done around 10 migrations on this project. I can still log in as a user. So why can't I choose a database context and migrate my remodeling to localdb?

Any suggestions would be appreciated.

Dave
  • 687
  • 7
  • 15

1 Answers1

0

The answer was quite simple. The beginning of this doc Design-time DbContext Creation illustrates the call chain for getting DbContext

Looking at Visual Studio Solution Explorer. It was easy to follow the path: Solution Explorer

So the correct command was:

PM> Add-Migration InitialCreate -Context rollbase.Data.ApplicationDbContext

It seems ridiculously easy now.

Dave
  • 687
  • 7
  • 15