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.
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.
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly