After adding the EntityFramework nuget package to my ASP.NET Core project and creating our DbContext(see below) I was ready to generate a first migration :
public class BloggingContext : DbContext | |
{ | |
public BloggingContext(DbContextOptions<BloggingContext> options) | |
: base(options) | |
{ } | |
public DbSet<Blog> Blogs { get; set; } | |
public DbSet<Post> Posts { get; set; } | |
} | |
public class Blog | |
{ | |
public int BlogId { get; set; } | |
public string Url { get; set; } | |
public List<Post> Posts { get; set; } | |
} | |
public class Post | |
{ | |
public int PostId { get; set; } | |
public string Title { get; set; } | |
public string Content { get; set; } | |
public int BlogId { get; set; } | |
public Blog Blog { get; set; } | |
} |
I opened the Package Manager Console in Visual Studio, browsed to the project folder(!), and executed dotnet ef. I ended up with the following error message:
dotnet : No executable found matching command "dotnet-ef"
At line:1 char:1
+ dotnet ef
+ ~~~~~~~~~
+ CategoryInfo : NotSpecified: (No executable f...and "dotnet-ef":String) [], RemoteExcep
tion
+ FullyQualifiedErrorId : NativeCommandError
To solve this install the following 2 extra packages:
- Microsoft.EntityFrameworkCore.Tools: Contains the Entity Framework Core Package Manager Console Tools. Includes Scaffold-DbContext, Add-Migration, and Update-Database.
- Microsoft.EntityFrameworkCore.Tools.DotNet: Contains the Entity Framework Core .NET Command-line Tools. Includes dotnet-ef.
Remark: When trying to install the Microsoft.EntityFrameworkCore.Tools.DotNet package, I got the following error message:
I solved it by adding the package directly to the csproj file:
<ItemGroup> | |
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.2" /> | |
</ItemGroup> |
After doing that run a dotnet restore first. If you now try to execute dotnet ef, it should work: