最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - How to fix the error of migrations in ASP.NET Core - Stack Overflow

programmeradmin1浏览0评论

I just started an ASP.NET crash course and I have a problem. In that course the teacher uses migration for SQL databases and he typed this this command in the Package Manager Console:

PM> add-migration "initialsetup"

In his IDE it was absolutely okay. His console didn't write anything wrong, and a file was created in the Data/Migrations folder (20200...initialsetup.cs).

In my console, I got this output:

PM> add-migration "initialsetup"

Build started...
Build succeeded.

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.

I tried to change the string but it give me the same error. Maybe it's because that video was published 3 years ago?

Thank you for help.

I just started an ASP.NET crash course and I have a problem. In that course the teacher uses migration for SQL databases and he typed this this command in the Package Manager Console:

PM> add-migration "initialsetup"

In his IDE it was absolutely okay. His console didn't write anything wrong, and a file was created in the Data/Migrations folder (20200...initialsetup.cs).

In my console, I got this output:

PM> add-migration "initialsetup"

Build started...
Build succeeded.

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.

I tried to change the string but it give me the same error. Maybe it's because that video was published 3 years ago?

Thank you for help.

Share Improve this question edited Feb 5 at 16:11 marc_s 755k184 gold badges1.4k silver badges1.5k bronze badges asked Feb 5 at 13:29 new csharp'ernew csharp'er 216 bronze badges 3
  • 3 It sounds like you have multiple instances of DB context. Can you edit your question and share some code so we can take a look at it? – LMA Commented Feb 5 at 13:34
  • Migration has two modes 1) Create 2) Update. You only have to run migration Create once when project is created which makes a new database. Then you only need to run Update if the fields(columns) of the database changes. – jdweng Commented Feb 5 at 22:39
  • Sounds like you have more than one model (in Data folder maybe?) that extends DbContext. ( YourContext : DbContext ) You probably only want one. EF is confused about which DB you are migrating. – browsermator Commented Feb 6 at 23:07
Add a comment  | 

1 Answer 1

Reset to default 0

When you encounter the error message "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," it typically means that there are multiple DbContext classes in your project. To specify which DbContext to use, you need to indicate the context in your command.

For PowerShell commands/PM commands:

Add-Migration InitialCreate -Context ContextName

-Context is one of the Common parameters when you use EF Core tools

https://learn.microsoft.com/en-us/ef/core/cli/powershell

发布评论

评论列表(0)

  1. 暂无评论