EF Core Error - No project was found. Change the current working directory or use the --project option
L

14

68

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 :(

Lard answered 24/3, 2017 at 5:0 Comment(4)
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.Mourant
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?Lard
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.Mourant
"Do we have no choice but to Install Visual Studio 2017 and start from scratch??" Sadly and eventually, yes.Unalienable
C
15

Instead of:

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

try:

"tools": {
      "Microsoft.EntityFrameworkCore.Tools.DotNet": {
      "version": "1.0.0-preview3-final"
  }},
Cyaneous answered 24/3, 2017 at 7:35 Comment(1)
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.Framework
S
120

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.

Sibylle answered 10/6, 2018 at 3:53 Comment(7)
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.Protozoal
This reply, cd MyProjectSubfolder is probably the "correct" answer for most of the people who hit this page. It worked for me :)Aquifer
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 cameTafia
Please make sure you added nuget Microsoft.EntityFrameworkCore.tools and Design packages.Sibylle
@AkshayMishra Thanks a lot, this was my problem and I had to use cd to point to the right project directory. day savior!Joanniejoao
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.Alsacelorraine
Hey thanks a lot. Almost looked over this answer and lo and behold. I was in the wrong dirViticulture
T
53

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.

Trousseau answered 25/9, 2019 at 10:18 Comment(7)
Thanks. That was my issue. After running the "dir" command it initially looked like the correct folder but then when I looked through Windows Explorer I saw that I needed to go one more folder deeper to get to the location of the .csproj file. Went to that directory and then ef migrations add command ran.Biodegradable
Glad to help youTrousseau
Had the same issue! Thanks mate. Is there any way to see the current dir instead of the nonsense "PM>"?Crampon
@Crampon glad to help you , did you check this #608170?Trousseau
As simply as that :). Thank youGlutamine
So nice to help you @GlutamineTrousseau
If it helped , please dont forget to buymeacoffee buymeacoffee.com/tahatemuri thanksTrousseau
C
15

Instead of:

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

try:

"tools": {
      "Microsoft.EntityFrameworkCore.Tools.DotNet": {
      "version": "1.0.0-preview3-final"
  }},
Cyaneous answered 24/3, 2017 at 7:35 Comment(1)
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.Framework
H
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
Hampstead answered 26/8, 2018 at 17:17 Comment(2)
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 go.microsoft.com/fwlink/?linkid=85172 8 for additional patterns supported at design time. I'll have a look at that linkBisexual
The fifth step is the most important.I did itTradesfolk
S
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.

Sindysine answered 28/3, 2017 at 9:32 Comment(1)
where to find this tools in visual studio ??Supplication
N
2

Just simply use this command.

Add-Migration InitialCreated -c bodoContext

No need to worry.

Nanananak answered 14/10, 2021 at 5:14 Comment(0)
K
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.

Koah answered 18/1, 2021 at 17:56 Comment(0)
M
1

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".

Maimonides answered 23/6, 2023 at 13:3 Comment(0)
L
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

Lucillelucina answered 9/5, 2017 at 20:10 Comment(0)
R
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
Ravish answered 17/3, 2023 at 14:34 Comment(0)
I
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

Iggie answered 10/4, 2023 at 8:34 Comment(0)
M
0

You are most likely in the directory that your IDE project is in. You need to change directories to the directory with the actual .Net project in.

In my case, I am using Rider, rather than Visual Studio, and on macOS. So there is an outer MyProject directory containing the Rider project file .idea, and things like .git, and .gitignore. Then there is an inner MyProject/MyProject directory containing all the .Net project stuff such as your source code.

So in my case, it was simply

cd MyProject

Then it worked. It created a Migrations directory containing the migration.

Mamoun answered 16/1, 2024 at 4:46 Comment(0)
L
-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
Lacker answered 14/8, 2021 at 13:35 Comment(1)
Please don't repeat answers. It only adds noise.Theorize
A
-1

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

dotnet ef database update -c ApplicationDbContext
Asiatic answered 7/4, 2023 at 13:57 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.