273

I'm using this MSDN Tutorial to run in VS2015 the command PM> Add-Migration MyFirstMigration -context BloggingContext that ran yesterday successfully but today it's giving the following error that is also pointed out by other users here. I even deleted the Migrations folder from solution explorer and the corresponding db from SQL Express 2014 on Win 8.1 but same error. Even if I run Add-Migration MyFirstMigration I get same error:

Add-Migration : The term 'Add-Migration' is not recognized as the name of a cmdlet, function, script file, or operable program. 
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ Add-Migration MyFirstMigration -context BloggingContext
+ ~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Add-Migration:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Note: I'm using latest version of ASP.NET Core 1.0 and VS2015 - Update 3 released on June 27, 2016.

UPDATE

The following commands work fine from the project directory in the windows explorer when using command window:

> dotnet ef migrations add MyFirstMigration --context BloggingContext
> dotnet ef database update --context BloggingContext

UPDATE 2a

Following is the project.json file:

{
  "userSecretsId": "aspnet-ASPCore_RTM_CodeFirst_test-bef835d9-9831-41a8-bc3a-cd2f1477a880",

  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.0",
      "type": "platform"
    },
    "Microsoft.AspNetCore.Authentication.Cookies": "1.0.0",
    "Microsoft.AspNetCore.Diagnostics": "1.0.0",
    "Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore": "1.0.0",
    "Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0",
    "Microsoft.AspNetCore.Mvc": "1.0.0",
    "Microsoft.AspNetCore.Razor.Tools": {
      "version": "1.0.0-preview2-final",
      "type": "build"
    },
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
    "Microsoft.AspNetCore.StaticFiles": "1.0.0",
    "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0",
    "Microsoft.EntityFrameworkCore.SqlServer.Design": {
      "version": "1.0.0",
      "type": "build"
    },
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
    "Microsoft.Extensions.Configuration.Json": "1.0.0",
    "Microsoft.Extensions.Configuration.UserSecrets": "1.0.0",
    "Microsoft.Extensions.Logging": "1.0.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.Extensions.Logging.Debug": "1.0.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0",
    "Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
      "version": "1.0.0-preview2-final",
      "type": "build"
    },
    "Microsoft.VisualStudio.Web.CodeGenerators.Mvc": {
      "version": "1.0.0-preview2-final",
      "type": "build"
    }
  },

  "tools": {
    "BundlerMinifier.Core": "2.0.238",
    "Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final",
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
    "Microsoft.Extensions.SecretManager.Tools": "1.0.0-preview2-final",
    "Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
      "version": "1.0.0-preview2-final",
      "imports": [
        "portable-net45+win8"
      ]
    }
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ]
    }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "runtimeOptions": {
    "configProperties": {
      "System.GC.Server": true
    }
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "Views",
      "Areas/**/Views",
      "appsettings.json",
      "web.config"
    ]
  },

  "scripts": {
    "prepublish": [ "bower install", "dotnet bundle" ],
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}
Community
  • 1
  • 1
nam
  • 21,967
  • 37
  • 158
  • 332
  • write please your project.json I want to see your packages – Bassam Alugili Jul 03 '16 at 21:06
  • @BassamAlugili per your request, I've added an UPDATE 2 in my post to add the relevant section of peoject.json file. Please let me know if you need more info – nam Jul 03 '16 at 22:10
  • You got an answer from @briceIam (EF team) I think it will works! – Bassam Alugili Jul 04 '16 at 06:43
  • @BassamAlugili I've added the entire content of the project.json file in UPDATE 2 section of my post. Please see my comments under the response from `@bricelam`. What I may be missing? – nam Jul 04 '16 at 15:39
  • For the benefit of searchers, I solved a similar problem by closing ALL instances of Visual Studio and starting back up again. Might save someone a bit of time. – JsAndDotNet Nov 28 '16 at 20:47
  • This issue should be resolved after closing Visual Studio. Refer to this link-https://learn.microsoft.com/en-us/ef/core/get-started/aspnetcore/new-db section ```Creating Database``` for more information. – Gaurav Arora Jan 10 '17 at 20:39
  • how to see project.json, I am in VS2017 and having the same problem – SHAH MD IMRAN HOSSAIN Jun 03 '18 at 07:56
  • @nam Please consider marking one of the answers below as accepted which will hopefully discourage people piling up new repeated answers. – Gert Arnold May 02 '21 at 20:34

24 Answers24

562

Just install Microsoft.EntityFrameworkCore.Tools package from nuget:

Install-Package Microsoft.EntityFrameworkCore.Tools -Version 7.0.7

You can also use this link to install the latest version: NuGet package link

.NET CLI command:

dotnet add package Microsoft.EntityFrameworkCore.Tools

If the problem still persists, try restarting Visual Studio.

Nasir Jafarzadeh
  • 6,123
  • 1
  • 13
  • 19
56

Try the following steps:

1) Open project.json file and Remove all Microsoft.EntityFrameworkCore.Tools references from dependencies and tools sections.

2) Close Package Manager Console (PMC) and restart Visual Studio

3) Add under dependencies section:

 "Microsoft.EntityFrameworkCore.Tools": {
  "version": "1.0.0-preview2-final",
  "type": "build"
 }

4) Add under tools section

"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"

5) Restart again Visual Studio 2015

6) Open the PMC and type

Add-Migration $Your_First_Migration_Name$

This happen because the PMC recognize the tools when Visual Studio is starting.

Gianpolo
  • 966
  • 8
  • 19
52

Ensure Microsoft.EntityFrameworkCore.Tools is referenced in the dependencies section of your project.json. NuGet won't load the Package Manager Commands from the tools section. (See NuGet/Home#3023)

{
  "dependencies": {
    "Microsoft.EntityFrameworkCore.Tools": {
      "version": "1.0.0-preview2-final",
      "type": "build"
    }
  }
}
bricelam
  • 28,825
  • 9
  • 92
  • 117
  • 2
    I've copied the entire project.json file content in UPDATE 2 of my post. The `Microsoft.EntityFrameworkCore.Tools` is inside dependencies section as well. The `Add-Migration` and `Update-Database` commands , as instructed in the tutorial worked night before. But when I tried them next morning after making minor changes to my model and tried these commands again they gave the error message described in my post. I even deleted the Migration folder and the corresponding database from the SQL Server but I got the same error message after running these commands. What I may be missing? – nam Jul 04 '16 at 15:33
  • I also have these symptoms. I'm using Visual Studio 2015 Update 3 – Kolya_Net Jul 05 '16 at 06:23
  • Try [filing a bug on NuGet](https://github.com/NuGet/Home/issues/new) with reproducible steps and lots of environment information. I've mentioned issues in this area before the team, but they're never able to repro them. – bricelam Jul 06 '16 at 03:39
  • 1
    Weird, this solves my problem if I remove it from `tools` and re-add it after. Simply moving it to `dependencies` will result in the following error message: Cannot execute this command because 'Microsoft.EntityFrameworkCore.Tools' is not installed in project 'src\. Add 'Microsoft.EntityFrameworkCore.Tools' to the 'tools' section in project.json. See http://go.microsoft.com/fwlink/?LinkId=798221 for more details. – Thorsten Westheider Jul 31 '16 at 13:40
  • I'm using VS2017 ASP.NET Core and had the same problem. This answer resolved it as well! – Westerlund.io Jun 17 '17 at 06:03
  • it says Core is not compatible with .net framework 4.5.2 i am still getting this error – user1735921 Mar 18 '21 at 15:03
17

same issue...resolved by dong the following

1.) close pm manager 2.) close Visual Studio 3.) Open Visual Studio 4.) Open pm manager

seems the trick is to close PM Manager before closing VS

Bhail
  • 710
  • 8
  • 14
  • Yes reloading PM and VS is a solution, It happens when we open project in latest visual studio that was previously created in older one.By opening such code, VS tries few auto tweaks.Those tweaks can be seen effected properly, only by restarting PM and VS. – Malik Khalil Nov 04 '18 at 10:39
16

From Asp.Net Core 5.0 perspective

In the Migrations project Install the following: NuGets to be installed in Migrations project

  • Add a reference to Data Access Layer (DAL) project (ex: if there is reference to Auth models)

In the Startup/Api project Install the following: NuGets to be installed in startup/Api project

  • Add a reference to your Migrations project.
  • Add a reference to the Data Access Layer (DAL) project if you have one.

Ensure that you have added DbContext derived type to the services collection by using following line of code in Startup.ConfigureServices(IServiceCollection services):

services.AddDbContext<YourDbContext>(options => 
                  options.UseSqlServer(Configuration.GetConnectionString("YourDb"),
                                      x=>x.MigrationsAssembly("Your.Migrations")));
Sharath
  • 516
  • 1
  • 8
  • 19
11

I just had this problem too. I closed and opened VS2015 and it "fixed" the issue...

Josh
  • 6,944
  • 8
  • 41
  • 64
11

This is what worked for me: From Visual Studio click on

Tools --> NuGet Package Manager --> Package Manager Console

enter image description here

Then you can run Add-Migration, for example:

Add-Migration InitialCreate
Seif
  • 701
  • 4
  • 13
  • 32
9

You have to know what is your Entity-Framework version. Also after that you have to check project.json and control these sections:

In Dependencies

check:

Microsoft.EntityFrameworkCore.Tools": {
  "version": "1.0.0-preview2-final",
  "type": "build"
},

This section:

"version": "1.0.0-preview2-final",

is related with version of your Entity-Framework and you have to change this with that.

After that the second section of the proj.json is here, In the Tools section of JSON you have:

"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",

Also this part of code is related with your Entity-Framework and last part of code in Dependencies section.

Note: After do this issues you should to close CMD and restart visual studio.

AmirReza-Farahlagha
  • 1,204
  • 14
  • 26
7

Just try init Microsoft.EntityFrameworkCore.Tools. In PM execute

C:\Users\<username>\.nuget\packages\Microsoft.EntityFrameworkCore.Tools\1.0.0-preview2-final\tools\init.ps1.

It helped me with the same problem. A version of the tools might be different. It`s depended of what you use in your project.

trueboroda
  • 2,650
  • 26
  • 24
  • 1
    For me, I use a relative path .\packages\Microsoft.EntityFrameworkCore.Tools.1.1.0\tools\init.ps1 This is because the PM's Current Directory is already the Solution directory, but for me the Tools package is part of the Solution. Personally I think this is the best solution since there is no restarting, editing, reinstalling. It takes an instant, and it works. – William Leader Sep 01 '17 at 20:35
6

​What I had to do...

1) Tools -> Nuget Package Manger -> Package Manager Settings

2) General Tab

3) Clear All NuGet Cache(s)

4) Restart Visual Studio

5

I had this problem in Visual Studio 2013. I reinstalled NuGet Package Manager:

https://marketplace.visualstudio.com/items?itemName=NuGetTeam.NuGetPackageManagerforVisualStudio2013

David Sopko
  • 5,263
  • 2
  • 38
  • 42
4

I ran into the same issue. Most of my projects had the same thing in tools.

"tools": {
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
}

This worked fine on all but one project. I changed the entry in tools to

"tools": {
    "Microsoft.EntityFrameworkCore.Tools": {
    "version": "1.0.0-preview2-final",
    "type": "build"
  }
}

And then ran dotnet restore. After the restore completed, Add-Migration worked normally.

smulholland2
  • 1,143
  • 2
  • 14
  • 28
3

These are the steps I followed and it solved the problem

1)Upgraded my Power shell from version 2 to 3

2)Closed the PM Console

3)Restarted Visual Studio

4)Ran the below command in PM Console dotnet restore

5)Add-Migration InitialMigration

It worked !!!

Karthik
  • 691
  • 1
  • 5
  • 16
3

In my case I added dependency via Nuget:

Microsoft.EntityFrameworkCore.Tools

And then run via Package Manager Console:

add-migration Initial -Context "ContextName" -StartupProject "EntryProject.Name" -Project "MigrationProject.Name"
Erçin Dedeoğlu
  • 4,950
  • 4
  • 49
  • 69
2

I had the same problem and found that it was a Visual Studio versioning problem in the Solution file.

I was targeting:

VisualStudioVersion = 14.0.25123.0

But I needed to target:

VisualStudioVersion = 14.0.25420.1

After making that change directly to the Solution file, EF Core cmdlets started working in the Package Manager Console.

Joel Waymack
  • 136
  • 1
  • 3
  • Thank you for trying to help. I'm using version `14.0.25421.03 Update 3` that's even higher than what you're using. – nam Jul 08 '16 at 15:40
2

Try to Install these Packages:

  1. EntityFrameworkCore.Tools

  2. EntityFrameworkCore.Design

Rovid kashyap
  • 39
  • 1
  • 8
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 02 '22 at 17:37
2

Solution had been working fine for me and then stopped. After a little messing about I:

  1. Cleaned Solution
  2. Closed the solution and reopened
  3. Went straight to Package Manager Console and ran add-migration MigrationName
CF5
  • 1,123
  • 9
  • 19
1

I had this problem and none of the previous solutions helped me. My problem was actually due to an outdated version of powershell on my Windows 7 machine - once I updated to powershell 5 it started working.

Lee Cordell
  • 276
  • 3
  • 9
1

I think the answer needs updating in 2017, as MS have made some (breaking) changes that is detailed here.

https://github.com/aspnet/EntityFramework/issues/7053

To summarise, you will now need a reference to EntityFrameWorkCore.Tools.DotNet in the Tools Section as below

"Microsoft.EntityFrameworkCore.Tools.DotNet": "1.0.0"

I have also posted a working project.json file below in case some one runs in problems.

{
  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.1",
      "type": "platform"
    },
    "Microsoft.AspNetCore.Diagnostics": "1.0.0",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.AspNetCore.Razor.Tools": {
      "version": "1.0.0-preview2-final",
      "type": "build"
    },
    "Microsoft.AspNetCore.StaticFiles": "1.0.0",
    "Microsoft.AspNetCore.Mvc": "1.0.1",
    "Microsoft.EntityFrameworkCore.SqlServer": "1.0.1",
    "Microsoft.Extensions.Configuration.Json": "1.0.1",
    "Microsoft.EntityFrameworkCore.Tools": {
      "version": "1.0.0-preview2-final",
      "type": "build"
    }
  },

  "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final",
    "Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
    "Microsoft.EntityFrameworkCore.Tools.DotNet": "1.0.0"
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ]
    }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "runtimeOptions": {
    "configProperties": {
      "System.GC.Server": true
    }
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "web.config"
    ]
  },

  "scripts": {
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}
Francis
  • 1,798
  • 1
  • 26
  • 32
1

Go to package manager console(in visual studio) and execute below command

C:\Users\<YOUR_USER>\.nuget\packages\Microsoft.EntityFrameworkCore.Tools\<YOUR_INSTALLED_VERSION>\tools\init.ps1

Shivendra
  • 159
  • 1
  • 7
1

I tried doing all the above and no luck. I downloaded the latest .net core 2.0 package and ran the commands again and it worked.

Tyrone Moodley
  • 3,442
  • 1
  • 15
  • 16
  • Is .net core 2.0 stable now so a project can be upgraded from `1.1.1` to `2.0`? – nam Aug 15 '17 at 03:14
  • 1
    .NET Core 2.0 is available as of yesterday August 14, 2017 as a final release. https://blogs.msdn.microsoft.com/dotnet/2017/08/14/announcing-net-core-2-0/ – Tyrone Moodley Aug 15 '17 at 14:20
1

In my case there was an error in the solution which did not occur to me. I Tried rebuilding but did not worked. Here is what you should do

  • Close Visual Studio And Reopen.
  • Clean The Solution and Build it again.
  • It will show the error in the solution.
  • Resolve the error and Try again.

(Ps: Visual Studio 2019 .netCore 5)

raw_hitt
  • 719
  • 5
  • 21
1

I am blinded, but i tried to do it in "Developer Powershell", but need to open "Package Manager Console" instead and choose project, where your migrations located. Also, set project as an active by "Set as startup Project" in context menu of Solution Explorer.

Also, be sure you are setting connection string to your command. Otherwise, you need to add your connection to you Machine Environment Variables.

It is in case you are specifying it in you DbContext :

 protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseNpgsql("MyConnectionStringName");            
    }

So "MyConnectionStringName" should be set there as an environment variable name, and value - your connection string.

Hope, will help someone

Nigrimmist
  • 10,289
  • 4
  • 52
  • 53
-3

Mine was a very silly mistake!

Solution:

  • Re-open Visual Studio and Clone repo.
  • Don't forget to double-click solution file to load it.
DMur
  • 625
  • 13
  • 26