66

I am using Visual Studio 2015 and dotnet core and trying to develop an EF Core Code First project using Sqlite and this documentation / tutorial, which also uses Sqlite => NET Core - New Database

When I try to add an initial migration from the command line ( I am CD-ed into the folder that my data model project is located in) by issuing the following command

dotnet ef migrations add InitialMigration

...I get the following Error.

enter image description here

No project was found. Change the current working directory or use the --project option.

I even tried using the --project option like so.

> dotnet --project "C:\Shiva\EF\EFCFSqlite.Data.xproj"  ef migrations add InitialMigration

but that gives the following error.

Unknown option: --project
.NET Command Line Tools (1.0.0-preview2-003131)
Usage: dotnet [host-options] [command] [arguments] [common-options]

I noticed that the documentation is using .csproj file whereas my Project is showing a xproj file. Also the docs mention something about not using project.json anymore :(

Here's my project.json file.

{
  "version": "1.0.0-*",

  "dependencies": {
    "Microsoft.EntityFrameworkCore.Sqlite": "1.1.1",
    "Microsoft.EntityFrameworkCore.Sqlite.Design": "1.1.1",
    "NETStandard.Library": "1.6.1"
  },
  "tools": {
    "Microsoft.EntityFrameworkCore.Tools.DotNet":"1.0.0"
  },
  "frameworks": {
    "netstandard1.6": {
      "imports": "dnxcore50"
    }
  }

What has changed? Do we have no choice but to Install Visual Studio 2017 and start from scratch?? Is project.json and all this other stuff no longer honored?

Seems like a massive change to me if that's the case :(

Shiva
  • 20,575
  • 14
  • 82
  • 112
  • 1
    I don't know your issue but I'll point out the --project is in the wrong spot. `dotnet ef` is different than `dotnet` and that's the command that needs the --proj. Also if you're using the latest tooling, you will need to migrate from project.json to the .csproj format. If you install Visual Studio 2017 this WILL break the preview tooling that was available with VS 2015 so only install it if everyone working on the project is ready to switch. – Erik Noren Mar 24 '17 at 06:47
  • Thanks Erik. Is it possible to migrate from `project.json` to `.csproj` without needing to upgrade to Visual Studio 2017? Or do I have to absolutely do both the project upgrade and VS upgrade for this to work? – Shiva Mar 24 '17 at 19:34
  • 1
    I wouldn't move to .csproj unless you were going to use an IDE that supported it. Check out Gys' answer below. The preview tooling should still be set up for project.json configuration. In general, the tooling you'll want to keep at preview for project.json support but the other libraries you should be able to use latest versions. – Erik Noren Mar 24 '17 at 20:38
  • 1
    "Do we have no choice but to Install Visual Studio 2017 and start from scratch??" Sadly and eventually, [yes](http://stackoverflow.com/questions/38536978/is-project-json-deprecated#38537047). – trevorc Mar 24 '17 at 22:33

13 Answers13

108

sometimes you need to change the current directory in console/terminal eg:

PM> cd E:\Projects\CrossTest\
PM> dotnet ef migrations add InitialMigration

and Align your package versions. Either use preview1 packages or preview2. Mix of those are not supported.

Akshay Mishra
  • 1,535
  • 2
  • 15
  • 14
  • 5
    This was the case for me. I had to encase the entire path within the double quotes. `PM> cd "E:\Projects\CrossTest\"` and it worked like charm for me. – Virag Dilip Desai Aug 02 '18 at 12:55
  • 8
    This reply, `cd MyProjectSubfolder` is probably the "correct" answer for most of the people who hit this page. It worked for me :) – paulsm4 Nov 24 '18 at 23:41
  • 1
    tried defferent way, but no working, cd "C:\pp\EmployeeApp" and cd C:\pp\EmployeeApp then execute command - dotnet ef migrations add InitialCreate. same issue came – Sajith Jul 10 '19 at 13:06
  • Please make sure you added nuget Microsoft.EntityFrameworkCore.tools and Design packages. – Akshay Mishra Jul 10 '19 at 14:43
  • 2
    @AkshayMishra Thanks a lot, this was my problem and I had to use cd to point to the right project directory. day savior! – PatsonLeaner Mar 18 '20 at 08:40
  • 3
    This is the least intrusive answer, and worked for me as well using .Net Core 5.0. I just had to point to correct directory without changing any config. – stack underflow Dec 08 '20 at 05:23
50

It simply Means that

YOU ARE NOT IN CURRENT PROJECT DIRECTORY

I was facing the same issue when scaffolding existing database of MySql using this.

Command I was executing:

dotnet ef dbcontext scaffold "Server=123.1.1.1;Uid=abc;Pwd=abc;Database=myDB;Connection Timeout=20;Persist Security Info=False;Port=3306;Allow User Variables=True;Connect Timeout=120;" MySql.Data.EntityFrameworkCore -o Models

Causing the same error , then I checked current working directory inside package manager console and found incorrect.

In my case

enter image description here

Mean I was not in current project directory then after switching directory

cd SSGCApp

Now you are in project directory all good to run the Command.

TAHA SULTAN TEMURI
  • 4,031
  • 2
  • 40
  • 66
15

Instead of:

"tools": {
    "Microsoft.EntityFrameworkCore.Tools.DotNet":"1.0.0"
  },

try:

"tools": {
      "Microsoft.EntityFrameworkCore.Tools.DotNet": {
      "version": "1.0.0-preview3-final"
  }},
svick
  • 236,525
  • 50
  • 385
  • 514
Gys Rademeyer
  • 360
  • 2
  • 8
  • 10
    thanks, that why I highly not recommend anyone doesn't use ASP.NET Core until v3.x.x, it is dependency hell and version hell. – Cheung Jul 24 '17 at 04:03
12
  1. Add the nuget package Microsoft.EntityFrameworkCore.Tools
  2. Add the nuget package Microsoft.EntityFrameworkCore.Design
  3. Right-click your project file, select Edit and then add the following to the ItemGroup that contains PackageReference nodes

<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.3" />

(You can find the latest version by finding it in the Nuget Package manager)

  1. Open the Package Manage Console: Tools->Nuget Package Manager->Package Manager Console
  2. Type cd {path where your csproj file resides} (this is important)
  3. Now type dotnet ef migrations add InitialMigration
  • This helped me get a bit further. I get this now: Unable to create an object of type '***Context'. Add an implementation of 'IDesignTimeDbContextFactory<***Context>' to the project, or see https://go.microsoft.com/fwlink/?linkid=85172 8 for additional patterns supported at design time. I'll have a look at that link – willDaBeast Nov 27 '18 at 15:25
  • The fifth step is the most important.I did it – Farb Dec 24 '19 at 06:55
2

Just faced similar issue. Fixed by downgrading to 1.0.0-preview3-final

"tools": {
     "Microsoft.EntityFrameworkCore.Tools.DotNet": "1.0.0-preview3-final",    
}

and changing --project param to --startup-project

dotnet ef --startup-project <PATH_TO_PROJECT_DIRECTORY> migrations add <MIGRATION_NAME>

In global.json I also downgraded version to

 "sdk": {
     "version": "1.0.0-preview2-003131"
 }

This might be a temp workaround before migrating to csproj.

Artur Karbone
  • 1,456
  • 13
  • 11
2

Just simply use this command.

Add-Migration InitialCreated -c bodoContext

No need to worry.

Shakir Ahmed
  • 559
  • 4
  • 13
1

Apparently, it may sound funny, but in my case when I was getting this error I had the server-side of the app running. Basically, make sure that your app is not running at all when trying to create migrations. As I said, for me that was the cure. Might be a bit of advice for those who couldn't fix it by following the marked answer.

Marin
  • 73
  • 1
  • 10
0

The dotnet-ef command has moved.

You will need to add a reference to Microsoft.EntityFrameworkCore.Tools.DotNet AND Microsoft.EntityFrameworkCore.Design to your dependencies in project.json, then add Microsoft.EntityFrameworkCore.Tools.DotNet to the tools section and you should be good to go.

Cited from: http://errummwelluhh.blogspot.com

Community
  • 1
  • 1
susieloo_
  • 1,391
  • 1
  • 16
  • 22
0

If you're getting this error while working with the ABP Framework and trying to add new migration:

  • Check that you're executing the command in the EFCore project/folder e.g: C:\Users\rushas\source\repos\MyProject\aspnet-core> for cmd command
  • If you have more than one DbContext, make sure to append context name to your command. Use -Context for PowerShell command and --context for the rest of it. e.g: dotnet ef migrations add InitialMigration --context MyProjectDbContext
Ruslan
  • 162
  • 2
  • 13
0

I know this is old but it also helps to check the version of your .net so that your install the required version. Check the accepted answer here: Command dotnet ef not found

thp3loN2
  • 27
  • 2
0

You should be sure current directory on Context file's folder. You can check with use "dir". And that was different, so change directory with using "cd".

-1

Add references Microsoft.EntityFrameworkCore.Design and Microsoft.EntityFrameworkCore.Tools

Then run:

dotnet-ef migrations add InitialCreate --project ProjectName

or

dotnet ef migrations add InitialCreate --project ProjectName
devknight
  • 47
  • 3
-1

for mac os i did like this.. working fine for me.

dotnet ef database update -c ApplicationDbContext
sweetnandha cse
  • 705
  • 7
  • 16