Visual Studio Package Manager Console

This file will log all operations when you operate Visual Studio. Open the ActiveLog.xml file and search 'error' in this file to find out which package caused this problem. Then open Visual Studio to find the package in Tools - NuGet Package Manager - Packages Manager Console with command 'Get-Package -Filter packagename -ListAvailable'.

-->

The Package Manager Console (PMC) tools for Entity Framework Core perform design-time development tasks. For example, they create migrations, apply migrations, and generate code for a model based on an existing database. The commands run inside of Visual Studio using the Package Manager Console. These tools work with both .NET Framework and .NET Core projects.

I have the same exact issue with Visual Studio 2015 RTM and Visual Studio 2013 with Update 5. I reproduced it simply by accessing the Package Manager Console from within Visual Studio 2015/2013. As soon as I access it I get the error. The console is built into Visual Studio on Windows. Open your project or solution in Visual Studio, and then open the Package Manager Console in Visual Studio by navigating to Tools NuGet Package Manager Package Manager Console. By default, console commands operate against a specific package source and project as set in the control at the. The Ctrl + Tab could help us leave Package Manager Console window to other window, but it will not close it. If you want to close the Package Manager Console window, please use SHIFT + ESC to close current opened window in Visual Studio. I have tested in my Visual Studio 2015. I then thought to try EF under a test console project in.Net Framework 4.7.2. When VS loads that project during a fresh launch, I get roughly the same issue. &: The term 'C: Users source repos ConsoleApp1 packages EntityFramework.6.4.4 tools init.ps1' is not recognized as the name of a cmdlet, function, script file, or operable.

If you aren't using Visual Studio, we recommend the EF Core Command-line Tools instead. The .NET Core CLI tools are cross-platform and run inside a command prompt.

Installing the tools

Install the Package Manager Console tools by running the following command in Package Manager Console:

Update the tools by running the following command in Package Manager Console.

Verify the installation

Verify that the tools are installed by running this command:

The output looks like this (it doesn't tell you which version of the tools you're using):

Using the tools

Before using the tools:

  • Understand the difference between target and startup project.
  • Learn how to use the tools with .NET Standard class libraries.
  • For ASP.NET Core projects, set the environment.

Target and startup project

The commands refer to a project and a startup project.

  • The project is also known as the target project because it's where the commands add or remove files. By default, the Default project selected in Package Manager Console is the target project. You can specify a different project as target project by using the --project option.

  • The startup project is the one that the tools build and run. The tools have to execute application code at design time to get information about the project, such as the database connection string and the configuration of the model. By default, the Startup Project in Solution Explorer is the startup project. You can specify a different project as startup project by using the --startup-project option.

The startup project and target project are often the same project. A typical scenario where they are separate projects is when:

  • The EF Core context and entity classes are in a .NET Core class library.
  • A .NET Core console app or web app references the class library.

It's also possible to put migrations code in a class library separate from the EF Core context.

Other target frameworks

The Package Manager Console tools work with .NET Core or .NET Framework projects. Apps that have the EF Core model in a .NET Standard class library might not have a .NET Core or .NET Framework project. For example, this is true of Xamarin and Universal Windows Platform apps. In such cases, you can create a .NET Core or .NET Framework console app project whose only purpose is to act as startup project for the tools. The project can be a dummy project with no real code — it is only needed to provide a target for the tooling.

Why is a dummy project required? As mentioned earlier, the tools have to execute application code at design time. To do that, they need to use the .NET Core or .NET Framework runtime. When the EF Core model is in a project that targets .NET Core or .NET Framework, the EF Core tools borrow the runtime from the project. They can't do that if the EF Core model is in a .NET Standard class library. The .NET Standard is not an actual .NET implementation; it's a specification of a set of APIs that .NET implementations must support. Therefore .NET Standard is not sufficient for the EF Core tools to execute application code. The dummy project you create to use as startup project provides a concrete target platform into which the tools can load the .NET Standard class library.

ASP.NET Core environment

To specify the environment for ASP.NET Core projects, set env:ASPNETCORE_ENVIRONMENT before running commands.

Starting in EF Core 5.0, additional arguments can also be passed into Program.CreateHostBuilder allowing you to specify the environment on the command-line:

Common parameters

The following table shows parameters that are common to all of the EF Core commands:

ParameterDescription
-Context <String>The DbContext class to use. Class name only or fully qualified with namespaces. If this parameter is omitted, EF Core finds the context class. If there are multiple context classes, this parameter is required.
-Project <String>The target project. If this parameter is omitted, the Default project for Package Manager Console is used as the target project.
-StartupProject <String>The startup project. If this parameter is omitted, the Startup project in Solution properties is used as the target project.
-Args <String>Arguments passed to the application. Added in EF Core 5.0.
-VerboseShow verbose output.

To show help information about a command, use PowerShell's Get-Help command.

Console

Tip

The Context, Project, and StartupProject parameters support tab-expansion.

Add-Migration

Adds a new migration.

Parameters:

ParameterDescription
-Name <String>The name of the migration. This is a positional parameter and is required.
-OutputDir <String>The directory use to output the files. Paths are relative to the target project directory. Defaults to 'Migrations'.
-Namespace <String>The namespace to use for the generated classes. Defaults to generated from the output directory. Added in EF Core 5.0.

The common parameters are listed above.

Drop-Database

Drops the database.

Parameters:

ParameterDescription
-WhatIfShow which database would be dropped, but don't drop it.

Visual Studio Package Manager Console Update

The common parameters are listed above.

Get-DbContext

Lists and gets information about available DbContext types.

Visual Studio Package Manager Console

The common parameters are listed above.

Get-Migration

Lists available migrations. Added in EF Core 5.0.

Parameters:

ParameterDescription
-Connection <String>The connection string to the database. Defaults to the one specified in AddDbContext or OnConfiguring.
-NoConnectDon't connect to the database.

The common parameters are listed above.

Remove-Migration

Removes the last migration (rolls back the code changes that were done for the migration).

Parameters:

Visual Studio Package Manager Console Mac

ParameterDescription
-ForceRevert the migration (roll back the changes that were applied to the database).
Visual Studio Package Manager Console

The common parameters are listed above.

Scaffold-DbContext

Generates code for a DbContext and entity types for a database. In order for Scaffold-DbContext to generate an entity type, the database table must have a primary key.

Parameters:

ParameterDescription
-Connection <String>The connection string to the database. For ASP.NET Core 2.x projects, the value can be name=<name of connection string>. In that case the name comes from the configuration sources that are set up for the project. This is a positional parameter and is required.
-Provider <String>The provider to use. Typically this is the name of the NuGet package, for example: Microsoft.EntityFrameworkCore.SqlServer. This is a positional parameter and is required.
-OutputDir <String>The directory to put files in. Paths are relative to the project directory.
-ContextDir <String>The directory to put the DbContext file in. Paths are relative to the project directory.
-Namespace <String>The namespace to use for all generated classes. Defaults to generated from the root namespace and the output directory. Added in EF Core 5.0.
-ContextNamespace <String>The namespace to use for the generated DbContext class. Note: overrides -Namespace. Added in EF Core 5.0.
-Context <String>The name of the DbContext class to generate.
-Schemas <String[]>The schemas of tables to generate entity types for. If this parameter is omitted, all schemas are included.
-Tables <String[]>The tables to generate entity types for. If this parameter is omitted, all tables are included.
-DataAnnotationsUse attributes to configure the model (where possible). If this parameter is omitted, only the fluent API is used.
-UseDatabaseNamesUse table and column names exactly as they appear in the database. If this parameter is omitted, database names are changed to more closely conform to C# name style conventions.
-ForceOverwrite existing files.
-NoOnConfiguringDon't generate DbContext.OnConfiguring. Added in EF Core 5.0.
-NoPluralizeDon't use the pluralizer. Added in EF Core 5.0.

The common parameters are listed above.

Example:

Example that scaffolds only selected tables and creates the context in a separate folder with a specified name and namespace:

The following example reads the connection string from the project's configuration possibly set using the Secret Manager tool.

Visual

Script-DbContext

Generates a SQL script from the DbContext. Bypasses any migrations. Added in EF Core 3.0.

Parameters:

ParameterDescription
-Output <String>The file to write the result to.

The common parameters are listed above.

Script-Migration

Generates a SQL script that applies all of the changes from one selected migration to another selected migration.

Parameters:

ParameterDescription
-From <String>The starting migration. Migrations may be identified by name or by ID. The number 0 is a special case that means before the first migration. Defaults to 0.
-To <String>The ending migration. Defaults to the last migration.
-IdempotentGenerate a script that can be used on a database at any migration.
-NoTransactionsDon't generate SQL transaction statements. Added in EF Core 5.0.
-Output <String>The file to write the result to. IF this parameter is omitted, the file is created with a generated name in the same folder as the app's runtime files are created, for example: /obj/Debug/netcoreapp2.1/ghbkztfz.sql/.

The common parameters are listed above.

Tip

The To, From, and Output parameters support tab-expansion.

The following example creates a script for the InitialCreate migration (from a database without any migrations), using the migration name.

Visual Studio Package Manager Console Mac

The following example creates a script for all migrations after the InitialCreate migration, using the migration ID.

Update-Database

Updates the database to the last migration or to a specified migration.

ParameterDescription
-Migration <String>The target migration. Migrations may be identified by name or by ID. The number 0 is a special case that means before the first migration and causes all migrations to be reverted. If no migration is specified, the command defaults to the last migration.
-Connection <String>The connection string to the database. Defaults to the one specified in AddDbContext or OnConfiguring. Added in EF Core 5.0.

The common parameters are listed above.

Tip

The Migration parameter supports tab-expansion.

The following example reverts all migrations.

The following examples update the database to a specified migration. The first uses the migration name and the second uses the migration ID and a specified connection:

Additional resources