.NET APIs

Microsoft Learn

Entity Framework Core Providers

Entity Framework Core (EF Core) is a modern object-relational mapper (ORM) for .NET. It allows developers to work with databases using .NET objects, eliminating the need for most of the data-access code they typically would have to write.

EF Core supports a variety of database providers, allowing you to use EF Core with different database systems. Each provider handles the translation of LINQ queries and other EF Core operations into the specific SQL dialect of the target database.

Supported Providers

Below is a list of common database providers for Entity Framework Core. Each provider requires a separate NuGet package installation.

Microsoft.EntityFrameworkCore.SqlServer

Provider for SQL Server. Supports SQL Server and Azure SQL Database.

View on NuGet

Npgsql.EntityFrameworkCore.PostgreSQL

Provider for PostgreSQL. Supports PostgreSQL.

View on NuGet

Pomelo.EntityFrameworkCore.MySql

Provider for MySQL. Supports MySQL.

View on NuGet

Microsoft.EntityFrameworkCore.Sqlite

Provider for SQLite. Supports SQLite, a lightweight, file-based database.

View on NuGet

Microsoft.EntityFrameworkCore.Cosmos

Provider for Azure Cosmos DB. Supports NoSQL document databases.

View on NuGet

Oracle.EntityFrameworkCore

Provider for Oracle Database.

View on NuGet

Configuring a Provider

You configure the database provider in your DbContext by overriding the OnConfiguring method or by using dependency injection.

Example using OnConfiguring:


using Microsoft.EntityFrameworkCore;

public class MyDbContext : DbContext
{
    public DbSet<Product> Products { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        // Example for SQL Server
        optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=MyDatabase;Trusted_Connection=True;");

        // Example for PostgreSQL
        // optionsBuilder.UseNpgsql("Host=localhost;Database=MyDatabase;Username=myuser;Password=mypassword");

        // Example for SQLite
        // optionsBuilder.UseSqlite("Data Source=mydatabase.db");
    }
}
            

Example using Dependency Injection (in Startup.cs or Program.cs):


using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;

// In ConfigureServices or similar method
public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<MyDbContext>(options =>
        options.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=MyDatabase;Trusted_Connection=True;"));
}
            

Key Considerations

For more detailed information on specific providers and their advanced configurations, please refer to the official documentation for each provider.