Installing Entity Framework Core

Prerequisites

  • To develop apps that target .NET Core 2.1, install the .NET Core 2.1 SDK. The SDK has to be installed even if you have the latest version of Visual Studio 2017.

  • To use Visual Studio for development of apps that target .NET Core 2.1, install Visual Studio 2017 version 15.7 or later.

  • To use Entity Framework 2.1 in ASP.NET Core applications, use ASP.NET Core 2.1. Applications that use earlier versions of ASP.NET Core must be updated to 2.1.

  • You can use Visual Studio 2015 for apps that target the .NET Framework 4.6.1 or later. But you need a version of NuGet that is aware of the .NET Standard 2.0 and its compatible frameworks. To get that in Visual Studio 2015, upgrade the NuGet client to version 3.6.0.

Get the Entity Framework Core runtime

To add EF Core runtime libraries to an application, install the NuGet package for the database provider you want to use. For a list of supported providers and their NuGet package names, see Database providers.

To install or update NuGet packages, use the .NET Core CLI, the Visual Studio Package Manager Dialog, or the Visual Studio Package Manager Console.

For ASP.NET Core 2.1 applications, the in-memory and SQL Server providers are automatically included, so there's no need to install them separately.

Tip

If you need to update an application that is using a third-party database provider, always check for an update of the provider that is compatible with the version of EF Core you want to use. For example, database providers for previous versions are not compatible with version 2.1 of the EF Core runtime.

.NET Core CLI

The following .NET Core CLI command installs or updates the SQL Server provider:

dotnet add package Microsoft.EntityFrameworkCore.SqlServer

You can indicate a specific version in the dotnet add package command, using the -v modifier. For example, to install EF Core 2.1.0 packages, append -v 2.1.0 to the command.

Visual Studio NuGet Package Manager Dialog

  • From the menu, select Project > Manage NuGet Packages

  • Click on the Browse or the Updates tab

  • To install or update the SQL Server provider, select the Microsoft.EntityFrameworkCore.SqlServer package, and confirm.

For more information, see NuGet Package Manager Dialog.

Visual Studio NuGet Package Manager Console

  • From the menu, select Tools > NuGet Package Manager > Package Manager Console

  • To install the SQL Server provider, run the following command in the Package Manager Console:

    Install-Package Microsoft.EntityFrameworkCore.SqlServer
    
  • To update the provider, use the Update-Package command.

  • To specify a specific version, use the -Version modifier. For example, to install EF Core 2.1.0 packages, append -Version 2.1.0 to the commands

For more information, see Package Manager Console.

Get Entity Framework Core tools

Besides the runtime libraries, you can install tools that can perform some EF Core-related tasks in your project at design time. For example, you can create migrations, apply migrations, and create a model based on an existing database.

Two sets of tools are available:

Although you can use the dotnet ef commands from the Package Manager Console, it's more convenient to use the Package Manager Console tools when you're using Visual Studio:

  • They automatically work with the current project selected in the Package Manager Console without requiring manually switching directories.
  • They automatically open files generated by the commands in Visual Studio after the command is completed.

Get the CLI tools

The dotnet ef commands are included in the .NET Core SDK, but to enable the commands you have to install the Microsoft.EntityFrameworkCore.Design package:

dotnet add package Microsoft.EntityFrameworkCore.Design	

For ASP.NET Core 2.1 apps, this package is included automatically.

As explained earlier in Prerequisites, you also need to install the .NET Core 2.1 SDK.

Important

Always use the version of the tools package that matches the major version of the runtime packages.

Get the Package Manager Console tools

To get the Package Manager Console tools for EF Core, install the Microsoft.EntityFrameworkCore.Tools package:

dotnet add package Microsoft.EntityFrameworkCore.Tools

For ASP.NET Core 2.1 apps, this package is included automatically.

Upgrading to EF Core 2.1

If you're upgrading an existing application to EF Core 2.1, some references to older EF Core packages may need to be removed manually:

  • Database provider design-time packages such as Microsoft.EntityFrameworkCore.SqlServer.Design are no longer required or supported in EF Core 2.1, but aren't automatically removed when upgrading the other packages.

  • The .NET CLI tools are now included in the .NET SDK, so the reference to that package can be removed from the .csproj file:

    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
    

For applications that target the .NET Framework and were created by earlier versions of Visual Studio, make sure that they are compatible with .NET Standard 2.0 libraries:

  • Edit the project file and make sure the following entry appears in the initial property group:

    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    
  • For test projects, also make sure the following entry is present:

    <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>