Entity Framework Core Installation
This document guides you through the process of installing Entity Framework Core (EF Core) into your .NET project. EF Core is a modern, cross-platform, open-source, and extensible version of the popular Microsoft Entity Framework data access technology.
Prerequisites
- .NET SDK installed. You can download it from the official .NET website.
- A .NET project (e.g., Console Application, ASP.NET Core Web Application, Class Library).
Installation Methods
EF Core can be installed using the .NET CLI or through Visual Studio's NuGet Package Manager.
1. Using the .NET CLI
The .NET CLI is a powerful command-line interface that allows you to manage your .NET projects, including installing packages.
Open your terminal or command prompt, navigate to your project's directory, and run the following commands:
-
Install the EF Core runtime: This package contains the core EF Core libraries.
dotnet add package Microsoft.EntityFrameworkCore
-
Install a database provider: You need a specific provider for the database you are using.
For SQL Server:
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
For SQLite:
dotnet add package Microsoft.EntityFrameworkCore.Sqlite
For PostgreSQL (using Npgsql):
dotnet add package Npgsql.EntityFrameworkCore.PostgreSQL
For MySQL (using Pomelo):
dotnet add package Pomelo.EntityFrameworkCore.MySql
For more providers, refer to the EF Core providers documentation.
-
Install EF Core Tools (optional but recommended): The tools package provides commands for scaffolding and migrations.
For command-line migrations:
dotnet tool install --global dotnet-ef
Or to install it as a local tool within your project:
dotnet new tool-manifest
dotnet add tool Microsoft.EntityFrameworkCore.Design
2. Using Visual Studio NuGet Package Manager
Visual Studio provides a user-friendly interface for managing NuGet packages.
-
In Visual Studio, right-click on your project in the Solution Explorer.
-
Select "Manage NuGet Packages...".
-
In the NuGet Package Manager window, go to the "Browse" tab.
-
Search for "Microsoft.EntityFrameworkCore" and install it.
-
Next, search for and install the specific database provider you need (e.g., "Microsoft.EntityFrameworkCore.SqlServer").
-
To install the EF Core Tools for migrations within Visual Studio, search for "Microsoft.EntityFrameworkCore.Design" and install it.
Note on .NET Core 3.1 and earlier
For .NET Core 3.1 and earlier versions, you might also need to install the "Microsoft.EntityFrameworkCore.Tools" package. For .NET 5 and later, the design-time functionality is included in the "Microsoft.EntityFrameworkCore.Design" package.
Verification
After installing the necessary packages, you can verify the installation by attempting to use EF Core features in your project, such as defining a DbContext and running migrations. Refer to the modeling data and migrations documentation for next steps.
Tip
Always ensure you are installing compatible versions of EF Core packages. Check the official EF Core release notes for compatibility information.
Important
The database provider package must be compatible with the EF Core runtime package you have installed. For example, if you install EF Core 7.0, you must install a SQL Server provider that also supports EF Core 7.0.