Convert Figma logo to code with AI

PomeloFoundation logoPomelo.EntityFrameworkCore.MySql

Entity Framework Core provider for MySQL and MariaDB built on top of MySqlConnector

2,675
382
2,675
174

Top Related Projects

13,632

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.

Entity Framework Core provider for PostgreSQL

MySQL Connector for .NET

2,790

.NET Transactional Document DB and Event Store on PostgreSQL

2,966

Linq to database provider.

Fast, Simple, Typed ORM for .NET

Quick Overview

Pomelo.EntityFrameworkCore.MySql is an Entity Framework Core provider for MySQL and MariaDB. It allows developers to use Entity Framework Core ORM with MySQL databases, providing a seamless integration between .NET applications and MySQL backend systems.

Pros

  • Full compatibility with Entity Framework Core features
  • Supports both MySQL and MariaDB databases
  • Active development and community support
  • Optimized for performance with MySQL-specific implementations

Cons

  • May have occasional compatibility issues with the latest EF Core versions
  • Limited support for some advanced MySQL-specific features
  • Potential performance overhead compared to raw SQL queries
  • Requires careful configuration for optimal performance in high-load scenarios

Code Examples

  1. Configuring the MySQL provider in DbContext:
public class MyDbContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseMySql("server=localhost;database=mydb;user=root;password=password",
            new MySqlServerVersion(new Version(8, 0, 25)));
    }
}
  1. Defining an entity with MySQL-specific column types:
public class Product
{
    public int Id { get; set; }
    [Column(TypeName = "varchar(100)")]
    public string Name { get; set; }
    [Column(TypeName = "decimal(18,2)")]
    public decimal Price { get; set; }
}
  1. Performing a LINQ query with MySQL-specific functions:
var results = await context.Products
    .Where(p => EF.Functions.Like(p.Name, "%apple%"))
    .OrderBy(p => EF.Functions.DateDiffDay(p.CreatedAt, DateTime.UtcNow))
    .ToListAsync();

Getting Started

  1. Install the NuGet package:

    dotnet add package Pomelo.EntityFrameworkCore.MySql
    
  2. Configure your DbContext:

    services.AddDbContext<MyDbContext>(options =>
        options.UseMySql(Configuration.GetConnectionString("DefaultConnection"),
            new MySqlServerVersion(new Version(8, 0, 25))));
    
  3. Use the context in your application:

    using (var context = new MyDbContext())
    {
        var products = await context.Products.ToListAsync();
        // Work with your data
    }
    

Competitor Comparisons

13,632

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.

Pros of EF Core

  • Broader database support, including SQL Server, PostgreSQL, and more
  • More frequent updates and active development from Microsoft
  • Extensive documentation and community support

Cons of EF Core

  • Lacks specific optimizations for MySQL
  • May require additional configuration for optimal MySQL performance
  • Larger footprint due to support for multiple databases

Code Comparison

Pomelo.EntityFrameworkCore.MySql:

services.AddDbContext<MyContext>(options =>
    options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString)));

EF Core:

services.AddDbContext<MyContext>(options =>
    options.UseSqlServer(connectionString));

The main difference is in the database provider method (UseMySql vs. UseSqlServer). Pomelo.EntityFrameworkCore.MySql is specifically tailored for MySQL, while EF Core supports multiple database providers.

Both repositories provide robust ORM solutions for .NET applications. EF Core offers a more versatile approach with support for various databases, while Pomelo.EntityFrameworkCore.MySql focuses on optimizing MySQL performance. The choice between the two depends on specific project requirements, such as database type, performance needs, and desired level of customization.

Entity Framework Core provider for PostgreSQL

Pros of efcore.pg

  • Better support for PostgreSQL-specific features like arrays, JSON, and full-text search
  • More frequent updates and active community involvement
  • Extensive documentation and examples for advanced PostgreSQL functionalities

Cons of efcore.pg

  • Limited to PostgreSQL, while Pomelo.EntityFrameworkCore.MySql supports both MySQL and MariaDB
  • Slightly steeper learning curve for developers not familiar with PostgreSQL-specific features
  • May require more configuration for optimal performance in certain scenarios

Code Comparison

efcore.pg:

services.AddDbContext<MyContext>(options =>
    options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")));

Pomelo.EntityFrameworkCore.MySql:

services.AddDbContext<MyContext>(options =>
    options.UseMySql(Configuration.GetConnectionString("DefaultConnection")));

Both providers offer similar syntax for basic configuration, but efcore.pg provides additional methods for PostgreSQL-specific features:

modelBuilder.Entity<MyEntity>()
    .HasPostgresExtension("uuid-ossp")
    .Property(e => e.Id)
    .HasDefaultValueSql("uuid_generate_v4()");

This code snippet demonstrates how efcore.pg allows easy integration of PostgreSQL extensions and functions, which is not available in Pomelo.EntityFrameworkCore.MySql due to database differences.

MySQL Connector for .NET

Pros of MySqlConnector

  • Lightweight and focused on MySQL connectivity
  • Supports asynchronous operations natively
  • Can be used with various ORMs or directly for database access

Cons of MySqlConnector

  • Requires additional setup for use with Entity Framework Core
  • May lack some Entity Framework Core-specific optimizations
  • Potentially steeper learning curve for developers used to EF Core

Code Comparison

MySqlConnector:

using MySqlConnector;

using var connection = new MySqlConnection(connectionString);
await connection.OpenAsync();
using var command = new MySqlCommand("SELECT * FROM Users", connection);
using var reader = await command.ExecuteReaderAsync();

Pomelo.EntityFrameworkCore.MySql:

using Microsoft.EntityFrameworkCore;

public class MyDbContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString));
    }
}

MySqlConnector provides a more direct approach to database access, while Pomelo.EntityFrameworkCore.MySql integrates seamlessly with Entity Framework Core, offering a higher level of abstraction. The choice between the two depends on project requirements, existing infrastructure, and developer preferences.

2,790

.NET Transactional Document DB and Event Store on PostgreSQL

Pros of Marten

  • Designed specifically for document databases (PostgreSQL), offering better support for document-oriented scenarios
  • Provides event sourcing capabilities out of the box
  • Offers a more flexible querying approach with LINQ and native PostgreSQL support

Cons of Marten

  • Limited to PostgreSQL, while Pomelo.EntityFrameworkCore.MySql supports MySQL and MariaDB
  • Smaller community and ecosystem compared to Entity Framework Core
  • Steeper learning curve for developers familiar with traditional ORM patterns

Code Comparison

Marten:

using (var session = store.LightweightSession())
{
    session.Store(new User { Name = "John" });
    await session.SaveChangesAsync();
}

Pomelo.EntityFrameworkCore.MySql:

using (var context = new MyDbContext())
{
    context.Users.Add(new User { Name = "John" });
    await context.SaveChangesAsync();
}

Both libraries provide similar functionality for basic CRUD operations, but Marten's API is more focused on document-oriented operations, while Pomelo.EntityFrameworkCore.MySql follows the traditional Entity Framework Core patterns.

2,966

Linq to database provider.

Pros of linq2db

  • Supports multiple database providers, not limited to MySQL
  • Offers better performance due to its lightweight nature and direct SQL generation
  • Provides more control over SQL queries and database-specific features

Cons of linq2db

  • Steeper learning curve compared to Entity Framework Core
  • Less extensive documentation and community support
  • Lacks some of the high-level abstractions and conveniences provided by EF Core

Code Comparison

Linq2db:

var query = from p in db.Products
            where p.CategoryId == 1
            select p;
var products = query.ToList();

Pomelo.EntityFrameworkCore.MySql:

var products = context.Products
    .Where(p => p.CategoryId == 1)
    .ToList();

Both examples achieve similar results, but linq2db offers more flexibility in query construction and optimization. Pomelo.EntityFrameworkCore.MySql, being built on Entity Framework Core, provides a more familiar and abstracted approach for developers accustomed to EF Core.

Fast, Simple, Typed ORM for .NET

Pros of ServiceStack.OrmLite

  • Lightweight and fast, with minimal overhead
  • Supports multiple database providers (MySQL, PostgreSQL, SQLite, etc.)
  • Offers a simple API for database operations

Cons of ServiceStack.OrmLite

  • Less integrated with Entity Framework Core ecosystem
  • May require more manual configuration for complex scenarios
  • Smaller community and fewer resources compared to EF Core

Code Comparison

Pomelo.EntityFrameworkCore.MySql:

public class MyDbContext : DbContext
{
    public DbSet<User> Users { get; set; }
    
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseMySql("connection_string");
    }
}

ServiceStack.OrmLite:

var dbFactory = new OrmLiteConnectionFactory("connection_string", MySqlDialect.Provider);
using (var db = dbFactory.Open())
{
    var users = db.Select<User>();
}

Both libraries provide ways to interact with MySQL databases, but they differ in their approach. Pomelo.EntityFrameworkCore.MySql integrates with Entity Framework Core, offering a more robust ORM experience with features like change tracking and lazy loading. ServiceStack.OrmLite, on the other hand, provides a simpler, more lightweight approach that may be easier to set up and use for basic database operations.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Pomelo.EntityFrameworkCore.MySql

Build status Stable release feed for official builds Nightly build feed for release builds Nightly build feed for debugging enabled builds

Pomelo.EntityFrameworkCore.MySql is the most popular Entity Framework Core provider for MySQL compatible databases. It supports EF Core up to its latest version and uses MySqlConnector for high-performance database server communication.

Compatibility

Dependencies

The following versions of MySqlConnector, EF Core, .NET (Core), .NET Standard and .NET Framework are compatible with published releases of Pomelo.EntityFrameworkCore.MySql:

ReleaseBranchMySqlConnectorEF Core.NET (Core).NET Standard.NET Framework
9.0.0-
preview.1
main>= 2.3.59.0.0-
preview.1
8.0+--
8.0.28.0-maint>= 2.3.58.0.28.0+--
7.0.07.0-maint>= 2.2.57.0.x6.0+--
6.0.36.0-maint>= 2.1.26.0.x6.0+--
5.0.45.0-maint>= 1.3.135.0.x3.0+2.1-
3.2.73.2-maint>= 0.69.10 < 1.0.03.1.x2.0+2.04.6.1+

Packages

Supported Database Servers and Versions

Pomelo.EntityFrameworkCore.MySql is tested against all actively maintained versions of MySQL and MariaDB. Older versions (e.g. MySQL 5.7) and other server implementations (e.g. Amazon Aurora) are usually compatible to a high degree as well, but are not tested as part of our CI.

Officially supported versions are:

  • MySQL 8.0
  • MariaDB 11.3
  • MariaDB 11.2
  • MariaDB 11.1
  • MariaDB 11.0
  • MariaDB 10.11 (LTS)
  • MariaDB 10.6 (LTS)
  • MariaDB 10.5 (LTS)
  • MariaDB 10.4 (LTS)

Schedule and Roadmap

MilestoneStatusRelease Date
9.0.0-preview.1Released2024-02-28 (see #1841)
8.0.2Released2024-03-16
7.0.0Released2023-01-16
6.0.3Released2024-03-16
5.0.4Released2022-01-22
3.2.7Released2021-10-04

Nightly Builds

To use nightly builds from our Azure DevOps feed, add a NuGet.config file to your solution root with the following content and enable prereleases:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <packageSources>
        <add key="pomelo-nightly" value="https://pkgs.dev.azure.com/pomelo-efcore/Pomelo.EntityFrameworkCore.MySql/_packaging/pomelo-efcore-public/nuget/v3/index.json" />
        <add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
    </packageSources>
</configuration>

Feeds

Feeds that contain optimized (Release configuration) builds:

  • https://pkgs.dev.azure.com/pomelo-efcore/Pomelo.EntityFrameworkCore.MySql/_packaging/pomelo-efcore-public/nuget/v3/index.json
  • https://www.myget.org/F/pomelo/api/v3/index.json

Feeds that contain debugging enabled unoptimized (Debug configuration) builds:

  • https://pkgs.dev.azure.com/pomelo-efcore/Pomelo.EntityFrameworkCore.MySql/_packaging/pomelo-efcore-debug/nuget/v3/index.json
  • https://www.myget.org/F/pomelo-debug/api/v3/index.json

The AZDO nupkg packages always contain .pdb files.

The MyGet nupkg packages only contain .pdb files for their debug builds. For optimized builds, the symbols are packed in a snupkg file and are available via the https://www.myget.org/F/pomelo/api/v2/symbolpackage/ symbol server URL.

All .pdb files use Source Link.

Getting Started

1. Project Configuration

Ensure that your .csproj file contains the following reference:

<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="8.0.2" />

2. Services Configuration

Add Pomelo.EntityFrameworkCore.MySql to the services configuration in your the Startup.cs file of your ASP.NET Core project:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        // Replace with your connection string.
        var connectionString = "server=localhost;user=root;password=1234;database=ef";

        // Replace with your server version and type.
        // Use 'MariaDbServerVersion' for MariaDB.
        // Alternatively, use 'ServerVersion.AutoDetect(connectionString)'.
        // For common usages, see pull request #1233.
        var serverVersion = new MySqlServerVersion(new Version(8, 0, 36));

        // Replace 'YourDbContext' with the name of your own DbContext derived class.
        services.AddDbContext<YourDbContext>(
            dbContextOptions => dbContextOptions
                .UseMySql(connectionString, serverVersion)
                // The following three options help with debugging, but should
                // be changed or removed for production.
                .LogTo(Console.WriteLine, LogLevel.Information)
                .EnableSensitiveDataLogging()
                .EnableDetailedErrors()
        );
    }
}

View our Configuration Options Wiki Page for a list of common options.

3. Sample Application

Check out our Integration Tests for an example repository that includes an ASP.NET Core MVC Application.

There are also many complete and concise console application samples posted in the issue section (some of them can be found by searching for Program.cs).

4. Read the EF Core Documentation

Refer to Microsoft's EF Core Documentation for detailed instructions and examples on using EF Core.

Scaffolding / Reverse Engineering

Use the EF Core tools to execute scaffolding commands:

dotnet ef dbcontext scaffold "Server=localhost;User=root;Password=1234;Database=ef" "Pomelo.EntityFrameworkCore.MySql"

Contribute

One of the easiest ways to contribute is to report issues, participate in discussions and update the wiki docs. You can also contribute by submitting pull requests with code changes and supporting tests.

We are always looking for additional core contributors. If you got a couple of hours a week and know your way around EF Core and MySQL, give us a nudge.

License

MIT