Entity Framework Core Installation
This guide covers the essential steps to install Entity Framework Core (EF Core) in your .NET applications.
Prerequisites
Ensure you have the following installed:
- .NET SDK (version 6.0 or later recommended)
- A code editor (e.g., Visual Studio, VS Code)
Installing EF Core via NuGet Package Manager
The most common way to add EF Core to your project is by using the NuGet Package Manager. You can do this through the .NET CLI or the Visual Studio GUI.
Using the .NET CLI
Open your terminal or command prompt in your project directory and run the following commands.
Core Packages
You'll typically need at least the Microsoft.EntityFrameworkCore
package. For a specific database provider, you'll need an additional package.
# Install the core EF Core package
dotnet add package Microsoft.EntityFrameworkCore
# Install a specific database provider (e.g., SQL Server)
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
# For tools like migrations
dotnet add package Microsoft.EntityFrameworkCore.Tools
Example: Installing EF Core for SQLite
dotnet add package Microsoft.EntityFrameworkCore.Sqlite
Using Visual Studio GUI
- Right-click on your project in the Solution Explorer.
- Select "Manage NuGet Packages...".
- Go to the "Browse" tab.
- Search for
Microsoft.EntityFrameworkCore
and install it. - Search for your desired database provider (e.g.,
Microsoft.EntityFrameworkCore.SqlServer
,Npgsql.EntityFrameworkCore.PostgreSQL
,Microsoft.EntityFrameworkCore.MySql
) and install it. - Search for
Microsoft.EntityFrameworkCore.Tools
and install it for migration commands.
Choosing a Database Provider
EF Core requires a database provider to interact with a specific database. Here are some common providers:
Database | NuGet Package | Notes |
---|---|---|
SQL Server | Microsoft.EntityFrameworkCore.SqlServer |
Microsoft's official SQL Server provider. |
SQLite | Microsoft.EntityFrameworkCore.Sqlite |
Lightweight, file-based database. |
PostgreSQL | Npgsql.EntityFrameworkCore.PostgreSQL |
Community-supported provider for PostgreSQL. |
MySQL | Pomelo.EntityFrameworkCore.MySql |
Community-supported provider for MySQL. |
Cosmos DB | Microsoft.Azure.Cosmos.EntityFrameworkCore |
For Azure Cosmos DB. |
Configuration
Once installed, you'll need to configure your DbContext
and the database provider in your application's startup or configuration code.
Example: Configuring SQL Server in Startup.cs
(ASP.NET Core)
using Microsoft.EntityFrameworkCore;
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<MyDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
// ... other services
}
Note:
The exact configuration method may vary slightly depending on your project type (.NET Core, .NET Framework, Console App, etc.).
Next Steps
After installation, you're ready to start modeling your data and interacting with your database. Proceed to the Getting Started guide.