.NET Documentation

Concepts | Data Access

Entity Framework Core Providers

Entity Framework Core (EF Core) is a modern object-relational mapper (ORM) for .NET that allows developers to work with databases using .NET objects. A key aspect of EF Core's flexibility is its provider model, which enables it to support a wide variety of database systems. This document explores the concept of EF Core providers and how they facilitate database interoperability.

What are EF Core Providers?

An EF Core provider is a NuGet package that bridges EF Core with a specific database system. It translates LINQ queries and other EF Core operations into the SQL dialect and commands understood by the target database. The provider also handles connection management, schema mapping, and other database-specific functionalities.

The core EF Core components are database-agnostic. When you add a database provider, you're essentially adding the necessary implementation for EF Core to communicate with that particular database.

Common EF Core Providers

Microsoft provides official providers for several popular databases. Additionally, the community contributes providers for many other data sources.

SQL Server Provider

The official Microsoft provider for SQL Server. It offers robust integration with the features and capabilities of Microsoft SQL Server.

PM> Install-Package Microsoft.EntityFrameworkCore.SqlServer

When configuring your DbContext, you'll use the UseSqlServer extension method:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    optionsBuilder.UseSqlServer("Your_Connection_String_Here");
}

PostgreSQL Provider

The Npgsql provider is the most popular and well-maintained provider for PostgreSQL. It's developed and supported by the community.

PM> Install-Package Npgsql.EntityFrameworkCore.PostgreSQL

Configuration:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    optionsBuilder.UseNpgsql("Your_Postgres_Connection_String_Here");
}

MySQL Provider

Several providers exist for MySQL, with Pomelo's EFCore.MySql and the MySql.Data.EntityFrameworkCore being common choices.

Pomelo EFCore.MySql:

PM> Install-Package Pomelo.EntityFrameworkCore.MySql

Configuration:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    optionsBuilder.UseMySql("Your_MySql_Connection_String_Here", ServerVersion.AutoDetect(new Version("8.0.0")));
}

SQLite Provider

The Microsoft.EntityFrameworkCore.Sqlite provider allows EF Core to work with SQLite databases, which are excellent for local development, testing, and embedded scenarios.

PM> Install-Package Microsoft.EntityFrameworkCore.Sqlite

Configuration:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    optionsBuilder.UseSqlite("Data Source=mydatabase.db");
}

Azure Cosmos DB Provider

This provider enables EF Core to interact with Azure Cosmos DB, a globally distributed, multi-model database service. It maps EF Core entities to Cosmos DB documents.

PM> Install-Package Microsoft.EntityFrameworkCore.Cosmos

Configuration:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    optionsBuilder.UseCosmos(
        "Your_Cosmos_DB_Account_Endpoint",
        "Your_Cosmos_DB_Account_Key",
        databaseName: "Your_Database_Name");
}

Other Providers

The EF Core ecosystem is rich with community-driven providers for databases like:

  • Oracle (EntityFrameworkCore.Oracle)
  • MongoDB (MongoDB.EntityFrameworkCore)
  • Firebase (EntityFrameworkCore.Firebase)
  • And many more...

You can typically find these providers on NuGet.org. Always check the provider's documentation for specific installation and configuration instructions.

How Providers Work

When EF Core needs to perform an operation (like querying data or saving changes), it:

  1. Takes your LINQ query or operation.
  2. Passes it to the configured provider.
  3. The provider translates this into the specific SQL or API calls for the target database.
  4. The provider executes the command against the database.
  5. It then takes the results from the database and maps them back to your .NET objects.

Key Components of a Provider

  • Query Translation: Converts LINQ expressions into database-specific query languages.
  • Database Commands: Generates SQL or other commands for data manipulation (INSERT, UPDATE, DELETE).
  • Connection Management: Handles opening, closing, and managing database connections.
  • Schema Mapping: Maps your .NET entity types and properties to database tables and columns.
  • Migration Support: Integrates with EF Core Migrations to manage database schema evolution.
  • Data Type Mapping: Ensures correct conversion between .NET data types and database data types.

Choosing the Right Provider

The choice of provider is dictated by the database you intend to use. Ensure that the provider you select is actively maintained and compatible with your EF Core version.

Conclusion

EF Core providers are fundamental to its ability to work with diverse data sources. By abstracting database-specific logic, they allow developers to focus on their application logic while EF Core handles the intricacies of data persistence across different database technologies.