56

Following this Microsoft Tutorial when I run the PM> Add-Migration MyFirstMigration command in VS2015 project created from the same tutorial I get the following error that I can't resolve:

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.

Point to note

  1. I'm using the above tutorial with the exception that I'm using Individual User Account authentication instead of No Authentication used in the tutorial.
  2. I've latest release of ASP.NeT Core 1.0 and VS2015-Update 3 on windows 8.1
  3. This is a freshly created project. No other DbContext was manually installed
2
  • 1
    The error text is describing the reason and what should you do. Commented Jul 2, 2016 at 16:29
  • Also, the code should be part of the question. The link is broken, rendering the question into an empty shell. Commented Aug 30, 2023 at 11:19

7 Answers 7

77

Running the following command (obtained from this article) and a response from @Maverik (from StackOverflow here) and a suggestion from @doctor above helped me resolved the issue. Thank you all for your help:

PM> Add-Migration MyFirstMigration -Context BloggingContext
Sign up to request clarification or add additional context in comments.

3 Comments

meanwhile the -Context changed to -c from what i could test
It should be --context in .net core 2.0.
is it possible that several contexts are same in name but diff only in namespace?
20

The error clearly explains to mention --context with db Context name if more than one DbContext. So try by mentioning your DbContext name.

dotnet ef migrations add Initial --context SampleDbContext

Hope this helps.

3 Comments

Isn't the issue that there are multiple db contexts? Do we need multiple contexts? How do we identify & delete them??
no. This is not an issue with multiple db contexts. We can use multiple db contexts, for example in multi tenant architecture it requires multiple db contexts. So there are situations where multiple db contexts required.
Didn't list what to do if you have multiple contexts. What if I have 10? Do I list --context 10 times???
16

If you need only update a identity schema existent, try it:

update-database -Context ApplicationDbContext

ApplicationDbContext = your identity context

Comments

4

that because you have two DbContext in your solution. First is default created when you creating project(ApplicationDbContext) and second your EF DbContext. Solution is described in error message just specify your EF DbContext

Comments

2

Add-Migration MyFirstMigration -Context DbContextName

It does work in my project.

Comments

1

Use below to commands:

PM> Add-Migration MyFirstMigration -Context YourDbContext

PM> update-database -Context YourDbContext

1 Comment

what is the difference between Add-Migration and dotnet ef Initial Migration?
1

[--context]

The DbContext class to use. Class name only or fully qualified with namespaces. If this option is omitted, EF Core will find the context class. If there are multiple context classes, this option is required.

Add-Migration MyMigration -context DataContextName

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.