Database Providers for Entity Framework Core
Entity Framework Core (EF Core) is designed to be extensible, and a key part of this extensibility is its support for various database providers. These providers translate LINQ queries and EF Core operations into the specific SQL or query language of the target database.
This page lists and provides links to common EF Core database providers, along with guidance on how to install and configure them.
Supported Providers
SQLite
An embedded, file-based relational database, ideal for local storage and testing.
Learn More »PostgreSQL (Npgsql)
A popular open-source relational database with a mature EF Core provider.
Learn More »Installing a Provider
To use a specific database provider, you need to install the corresponding NuGet package. For example, to use the SQL Server provider, you would typically run:
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
Or, if using Visual Studio:
- Right-click on your project in Solution Explorer.
- Select "Manage NuGet Packages...".
- Search for
Microsoft.EntityFrameworkCore.SqlServer
and install it.
Configuring a Provider
Once installed, you configure the provider in your DbContext
using the OnConfiguring
method or by passing configuration options to the constructor:
Using OnConfiguring
public class MyDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=MyDatabase;Trusted_Connection=True;");
}
// ... your DbSets ...
}
Using Dependency Injection (Recommended)
In modern .NET applications, it's common to configure EF Core services using dependency injection. You'd typically do this in your Startup.cs
or Program.cs
:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<MyDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
}
Ensure your appsettings.json
or equivalent configuration source contains the connection string:
{
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=MyDatabase;Trusted_Connection=True;"
}
}
Choosing a Provider
The choice of database provider depends on your project requirements, existing infrastructure, and team familiarity. Consider factors such as:
- Database Features: Does the database offer features your application needs (e.g., JSON support, geospatial capabilities)?
- Performance: Performance characteristics can vary significantly between databases.
- Licensing and Cost: Some databases are open-source and free, while others have commercial licenses.
- Community and Support: A strong community can provide valuable help and resources.
EF Core aims to provide a consistent experience across all providers, but there might be provider-specific nuances or advanced features you'll want to explore in their respective documentation.