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

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

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.

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

Instead of:
"tools": {
"Microsoft.EntityFrameworkCore.Tools.DotNet":"1.0.0"
},
try:
"tools": {
"Microsoft.EntityFrameworkCore.Tools.DotNet": {
"version": "1.0.0-preview3-final"
}},

Add the nuget package Microsoft.EntityFrameworkCore.Tools
Add the nuget package Microsoft.EntityFrameworkCore.Design
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)
Open the Package Manage Console: Tools->Nuget Package Manager->Package Manager Console
Type cd {path where your csproj file resides} (this is important)
Now type dotnet ef migrations add InitialMigration

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.

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.

Just simply use this command.
Add-Migration InitialCreated -c bodoContext
No need to worry.

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

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

Related

DotNet CLI Tool fails with "No executable found matching command "dotnet-migrate-2017"

I am trying to use migrate-2017 to migrate some csproj files to the new more concise vs2017 project format. I installed the tool from https://github.com/hvanbakel/CsprojToVs2017 using the command:
dotnet tool install --global Project2015To2017.Migrate2017.Tool
When I try to use it I get this:
C:\projects\Trilogy\Main>dotnet migrate-2017 wizard
No executable found matching command "dotnet-migrate-2017"
However, it appears to exist....
C:\projects\Trilogy\Main>dotnet tool list -g
Package Id Version Commands
------------------------------------------------------------------------
project2015to2017.migrate2017.tool 4.0.0 dotnet-migrate-2017
The global.json looks fine....
C:\projects\Trilogy\Main>type global.json
{
"sdk": {
"version": "2.1.602"
}
}
And so does the version:
C:\projects\Trilogy\Main>dotnet --version
2.1.602
I'm using dotnet CLI tools for the first time, so what obvious thing am I missing?
I don't think you're missing anything, but here's a few things to try:
Does running dotnet-migrate-2017 work (no dotnet required first)?
Does the tool exist under %userprofile%\.dotnet\tools?
Have you tried restarting the command prompt to force refresh your %PATH%?
Have you tried uninstalling and reinstalling the package?

How to change the output folder for migrations with asp.net Core?

Does anyone know how to change the output directory of the following command?
dotnet ef migrations add Initial --context EsportshubApi.Models.ApplicationDbContext
I tried to add the option:
--content-root-path 'Migrations/Identity'
But that doesn't do anything. There is a --data-dir option as well and something else with directory. But none of them is the output for migrations.
My problem is that I have 2 DbContexts so I want their migrations to be separated.
dotnet ef migrations add Initial --context EsportshubApi.Models.ApplicationDbContext -o YourFolderPath
dotnet ef migrations add
Adds a new migration.
Arguments:
Argument
Description
<NAME>
The name of the migration.
Options:
Option
Short
Description
--output-dir <PATH>
-o
The directory used to output the files. Paths are relative to the target project directory. Defaults to "Migrations".
--namespace <NAMESPACE>
-n
The namespace to use for the generated classes. Defaults to generated from the output directory. Added in EF Core 5.0.
Also here are the common options you can use with this command.
Source
For Package Manager Console run this command:
PM> Add-Migration 001 -OutputDir "Data/Migrations"
My structure is:
.AspCoreProject
-Data
-Migrations
20190721162938_001.cs
MainDbContextModelSnapshot.cs
Update:
For removing last migration use:
PM> Remove-Migration
Note: If the migration is already applied to the database, then you will get this error:
The migration '20190721162938_001' has already been applied to the database. Revert it and try again. If the migration has been applied to other databases, consider reverting its changes using a new migration.
Then run:
PM> Remove-Migration -Force
If your migration is not the last migration. first, rollback to the migration you need by Update-Database then delete all migration classes after that migration.
PM> Update-Database -Migration 001
This will revert all migrations after 001
In EF Core 5.0, you are free to move Migration files and changes their namespace manually. New migrations are created as siblings of the last migration. Alternatively, you can specify the directory at generation time as follows:
.Net core CLI
dotnet ef migrations add InitialCreate --output-dir Your/Directory
Package Manager Console
Add-Migration InitialCreate -OutputDir Your\Directory
EF Core 5.0 documentation
You just need to use -o Or --output option with your command,
To do so, you need to explore to your root project folder, eg: C:\project\SampleAPi\
and use this command
dotnet ef migrations add DbInitial --context SampleAPi.Infrastructure.DbContext -o Infrastructure/Migrations
and then
dotnet ef database update

Resolving dependencies using restore and build

I have several questions related to project.json, dependencies and the use of dotnet restore :
Where are located the packages when doing dotnet restore according to dependencies declared in project.json?
I have seen that one can dotnet restore --packages "myPackagesPath thus specifying where to locate its packages, but how can `dotnet build' knows where to fetch the dependencies afterwards? I have not seen any part of the project.json that specifies it.
is project.json the way to go when creating new project in dotnet core?
1: depending on your OS, it is in $USERDIR/.nuget/packages/
2: sorry, no idea on this one
3: yes, try it by doing: dotnet new it'll create a project.json

Is it possible to add a reference to NetCore library from a NetCore app without using the same solution?

I have a netstandard1.6 Project "MyLibrary"
I have a netcoreapp1.0 Project "MyApplication"
I have to reference "MyLibrary" from "MyApplication", without having those on the same solution.
Is it possible? How?
It's really confusing how DNX RC1 jumped into what Asp .Net 5 is right now, I can't seem to get going with it, I've read several articles and no documentation answered those questions to me.
With .Net Core, there are two ways to reference dependencies:
Reference another project in the "solution", where the solution is specified by a global.json file.
For example, the relevant part of your project.json could look like this:
"dependencies": {
"MyLibrary": { "target": "project" }
}
Reference a NuGet package from a known package source. Package source can be either web-based (like nuget.org), or it can be a directory. You can configure known package sources either locally or globally using a nuget.config file.
Example project.json excerpt:
"dependencies": {
"MyLibrary": "1.0.0"
}
For you, this means you have two options:
Add the library project to your "solution" by including it in your global.json.
Produce a NuGet package from your library (you can use dotnet pack for that), place it into a package source (probably just a directory) and configure that package source in a nuget.config file.

ASP.NET MVC 6 Project cannot find DNX Run time

When I try to build the project the error I get is as follows..
The Dnx Runtime package needs to be installed. See output window for more details
I have tried running both
dnvm upgrade
dnvm upgrade -r CoreClr
And when I run
dnvm list
I can see the run time installed which matches my project properties.
I have also tried the solution outlined here;
http://www.sblackler.net/2015/05/02/Up-And-Running-With-DNX-DNVM-DNU/
Anyone have any ideas what else I can try? Is there a way to explicitly point my project to the run time in the .dnx folder of my
C:\Users\<Username>\.dnx
folder?
If you are using the command line, you need to make sure that you also do dnvm use <version/label>.
On dnvm list, you'll see labels to the right, or you can use a specific version.
dnvm use default will use the default label.
dnvm use 1.0.0-beta4 will use the beta4 runtime specifically. (assuming you have it installed).
This might be a bit late, but better late than never ;)
clean up your dnx runtimes in C:\Users\USERNAME\.dnx and run dnvm upgrade. This should reinstall the latest runtimes. Or just use dnvm install <version> to install the ones you require.
Next, make sure your project references the right dnx runtime package in global.json depending on your preferences.
"sdk": {
"version": "1.0.0-beta7",
"runtime": "clr",
"architecture": "x86"
}
Check project properties to point to the specific runtime of your choice.
Also try and keep your dependencies in the same beta version to avoid anomalies.

Resources