3
   Add-Migration : A parameter cannot be found that matches parameter name 'Context'.
At line:1 char:15
+ Add-Migration -Context ManagementSystemContext
+               ~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Add-Migration], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Add-Migration

I'm trying to add migration to App but I got this error despite using this command:

PM> Add-Migration -Context ManagementSystemContext

I uninstalled EntityFrameWork.tools then reinstalled it and restarted visual studio and still the same error

ha33
  • 162
  • 1
  • 5
  • 16
  • You can see more details with `-Verbose` – klekmek Jan 27 '22 at 10:54
  • @Anu6is I have more than one context so I want to add migration to specific context – ha33 Jan 27 '22 at 10:59
  • Add-Migration **NameOfYourMigration** -Context ManagementSystemContext – Anu6is Jan 27 '22 at 11:00
  • @klekmek I tried and it gave the same error – ha33 Jan 27 '22 at 11:02
  • @Anu6is as you can see my question I have mentioned that I used command Add-Migration -Context ManagementSystemContext and still git the error – ha33 Jan 27 '22 at 11:03
  • Yes, and I added a migration name, which you omitted – Anu6is Jan 27 '22 at 11:04
  • @Anu6is it gives me the same error – ha33 Jan 27 '22 at 11:05
  • @Anu6is it worked smoothly in my last projects, I don't know what happened – ha33 Jan 27 '22 at 11:07
  • 1
    see if this helps - https://stackoverflow.com/a/32993262/11159372 ... by all means it should work though https://learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/providers?tabs=vs – Anu6is Jan 27 '22 at 11:12
  • PM> Add-Migration ManagementSystemContext – Amjad S. Jan 27 '22 at 11:12
  • 1. Please share your project file to check if you have installed matched package: `Microsoft.EntityFrameworkCore`, `Microsoft.EntityFrameworkCore.Design` and `Microsoft.EntityFrameworkCore.SqlServer`. 2. Please share your `ManagementSystemContext` and check if it extends `Microsoft.EntityFrameworkCore.DbContext`. 3. Plase share your Startup.cs about how do you register `ManagementSystemContext`. – Rena Jan 28 '22 at 02:44
  • Or you can try to use EF Core Tool. 1. Install by using: `PM>dotnet tool install --global dotnet-ef `. 2. run command for migration: `PM> dotnet ef migrations add MigrationName --context ManagementSystemContext--output-dir MigrationFileLocationName --project ProjectName`. – Rena Jan 28 '22 at 02:45

3 Answers3

7

I had the same problem and I couldn't realize what it is. After 2-3 restarts of the PC and the VS, making the same command

Add-Migration NameOfTheMigration -Context MyContext

it appear something in the lines of

"if you are using EntityFramework or EntityFrameworkCore, make sure that you type EntityFramework\Add-Migration, or EntityFrameworkCore\Add-Migration".

I did that and the migration was on.

So the command line looked like this:

EntityFrameworkCore\Add-Migration NameOfTheMigration -Context MyContext

I'm guessing that there were both, EF and EFCore, so the PMC was confused. Anyway, this is what worked for me.

ggeorge
  • 1,496
  • 2
  • 13
  • 19
  • EF6 appears to have its own prefix: "Both Entity Framework Core and Entity Framework 6 are installed. The Entity Framework Core tools are running. Use 'EntityFramework6\Add-Migration' for Entity Framework 6." – A N Nov 08 '22 at 20:06
0

After "Add-Migration" command EF first expects the name of the migration. You can cheek this documentation and code sample to see if you did something wrong. Also you can try to do it via .net core CLI instead of Package-Manager Console, like:

dotnet ef migrations add MigrationName --context ManagementSystemContext --output-dir Dir

Hope something will help.

vgnbk
  • 1
  • gives me this error : Could not execute because the specified command or file was not found. Possible reasons for this include: * You misspelled a built-in dotnet command. * You intended to execute a .NET program, but dotnet-ef does not exist. * You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH. – ha33 Jan 27 '22 at 19:40
  • You need to install the dotnet ef core CLI. `dotnet tool install --global dotnet-ef` in cmd/powershell. Then try again. – klekmek Jan 28 '22 at 12:26
0

If your project has two contexts as well as Multi Startup. You must do the following steps:

1- First, take the project out of Multi StartUp mode and set the project startup to your desired Startup.

  1. Then in the Package Manger Console section, type the following command:

    Add-Migration -Context ContextName

NOTE : Your default Project in package manger console must set as your DataAccess class libarary