In my post on migration from ASP.NET Core RC1 to RC2 I mentioned that there was a move from dnx to the .NET CLI. This post will be an overview this new platform and some of its capabilities.
Installation
If you already have ASP.NET Core RC2 installed then you already have the .NET CLI. If not head over to http://dot.net and click on Download .NET Core (or click here). This should take you to the proper download for your current OS. If you are on Windows and just want to install the command line tools the .NET Core SDK installer can be found here.
Installation Verification
Open a command prompt and run dotnet –version and if all is install and working you should see the version of the CLI you have installed. As of this writing I have version 1.0.0-preview1-002702 installed.
Hello World
Creating a new application that prints hello world to the console can be done without changing any code. First create a new directory and navigate to the new directory. Next execute the following commands.
dotnet new dotnet restore dotnet run
After the last command you should see “Hello World!” printed by a .NET console application that was created with the new command. restore uses project.json (for now) to download the packages need for the application. Finally run compiles and executes the application.
Basic Concepts
You may see the dotnet command referred to as a driver. All this means is that it is used to execute other commands. For example dotnet new is telling the drive to execute the new command (also termed verb). The CLI comes with a set of common commands out of the box that can be extended with more commands via NuGet on per project or on the system path for machine level commands.
Common Commands
The following is a list of common command pull using dotnet help
Command | Description |
---|---|
new | Initialize a basic .NET project |
restore | Restore dependencies specified in the .NET project |
build | Builds a .NET project |
publish | Publishes a .NET project for deployment (including the runtime) |
run | Compiles and immediately executes a .NET project |
test | Runs unit tests using the test runner specified in the project |
pack | Creates a NuGet package |
Adding Commands via NuGet
I am going to add the Entity Framework Core tool to the Hello World application created above as an example. To do this I am using Visual Studio Code, but you can use any editor you would like. All the changes needed will be made in project.json
In the dependencies section add the follow so that the needed packages will be downloaded when the dotnet restore command is run.
"Microsoft.EntityFrameworkCore.Tools": { "type": "build", "version": "1.0.0-preview1-final" }
Next add a tool section. The tool section how CLI discovers available commands.
"tools": { "Microsoft.EntityFrameworkCore.Tools": { "imports": ["portable-net451+win8"], "version": "1.0.0-preview1-final" } }
Finally in the frameworks section the imports needs to be change from dnxcore50. This change is needed for Entity Framework Core and may not be required if you are trying to use a different tool. The following is the full frameworks section.
"frameworks": { "netcoreapp1.0": { "imports": "portable-net451+win8" } }
After running dotnet restore to download the new dependencies dotnet ef can be used to access Entity Framework Core commands. Running dotnet ef without any other arguments will display the Entity Framework Core command help.
_/\__ ---==/ \\ ___ ___ |. \|\ | __|| __| | ) \\\ | _| | _| \_/ | //|\\ |___||_| / \\\/\\ Entity Framework .NET Core CLI Commands 1.0.0-preview1-20901 Usage: dotnet ef [options] [command] Options: -h|--help Show help information -v|--verbose Enable verbose output --version Show version information --framework <FRAMEWORK> Target framework to load --configuration <CONFIGURATION> Configuration under which to load Commands: database Commands to manage your database dbcontext Commands to manage your DbContext types migrations Commands to manage your migrations Use "dotnet ef [command] --help" for more information about a command.
Wrapping Up
Here are a couple of good references if you are just getting started. First is the documentation for the .NET CLI which can be found here. The second is this post by Sam Basu.
Keep an eye for a future post on the process of creating new tool that can be used with the .NET CLI.