Asp.Net EF Core error : More than one DbContext was found.

Dung Do Tien Jun 22 2021 159


I have created a project with Asp.Net Core 3.1 and using EF core 2.0 with code first. I have created a DBContext as below:

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

    protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
             base.OnModelCreating(modelBuilder);

             modelBuilder.Entity<ApplicationUser>()
             .ToTable("User");
            
             modelBuilder.Entity<IdentityRole>()
             .ToTable("Role");
    }
}

startup.cs

 public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<DBContext>(x => x.UseSqlServer(Configuration.GetConnectionString("SlaveConnection")));
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
}

And I try to run dotnet migration:

 dotnet ef migrations add CreateIdentity

But I got an error:

 More than one DbContext was found. Specify which one to use. Use the '-Context' parameter for PowerShell commands and the '--context' parameter for dotnet commands 

How can I resolve it?

Have 2 answer(s) found.
  • V

    Vuong Do Minh Jun 22 2021

    I think you are wrong about syntax. Please follow this syntax

     Add-Migration [-Name] <String> [-OutputDir <String>] [-Context <String>] [-Project <String>] [-StartupProject <String>] [-Environment <String>] [<CommonParameters>]

    you can try

     add-migration MyMigration -Context DBContext

    I hope it works for you.

  • T

    Thủy Lê Jun 22 2021

    I usually use the command below to add a migration FE core in Asp.net Core

     Add-Migration MyMigration --context DBContext

    I think your command has some mistake.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close