More than one DBContext was found - EF Core

I am trying to add new migrations in ASP.NET Core 3 web-api application which has already created database and using code-first approach, so when I trying to add new migrations, after creating Class and adding it in DBContext, getting this error, as shown below

"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? Thanks.

Asked by:- manish
: 416 At:- 4/29/2022 3:49:34 PM
ASP.NET Core Entity framework Core

1 Answers
profileImage Answered by:- vikas_jk

This error occurs when you have more than 1 class that inherits DbContext as base class, so to add new migration, you need to follow this command

Add-Migration <MigrationName> -context <DbContext_ToBeUsed>

using dotnet-cli

dotnet ef migrations add <your_migration_name> -c <your_context_class_name>

so for example, I have using AppDbContext, then considering your code, it would be like

 Add-Migration UserViews  -context AppDBContext 

Also, when you will run the "Update database" command, then also you need to mention DbContext name which you will be using, something like this.

Update-database -context AppDBContext 

That should work.

At:- 4/29/2022 3:58:52 PM
That worked, thanks for quick answer. 0
By : manish - at :- 4/29/2022 4:00:23 PM

Login/Register to answer
Register directly by posting answer/details

Full Name *

Email *

By posting your answer you agree on privacy policy & terms of use