AnointedAutomation.Repository.MySql 1.0.6

There is a newer version of this package available.
See the version list below for details.
dotnet add package AnointedAutomation.Repository.MySql --version 1.0.6
                    
NuGet\Install-Package AnointedAutomation.Repository.MySql -Version 1.0.6
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AnointedAutomation.Repository.MySql" Version="1.0.6" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AnointedAutomation.Repository.MySql" Version="1.0.6" />
                    
Directory.Packages.props
<PackageReference Include="AnointedAutomation.Repository.MySql" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add AnointedAutomation.Repository.MySql --version 1.0.6
                    
#r "nuget: AnointedAutomation.Repository.MySql, 1.0.6"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package AnointedAutomation.Repository.MySql@1.0.6
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=AnointedAutomation.Repository.MySql&version=1.0.6
                    
Install as a Cake Addin
#tool nuget:?package=AnointedAutomation.Repository.MySql&version=1.0.6
                    
Install as a Cake Tool

AnointedAutomation.Repository.MySql

Jesus is King ✝️

A generic repository and helper for MySQL on Entity Framework Core (Pomelo provider). GenericRepository<T> wraps any DbContext with async CRUD, paging, ordering and projection, and MySqlHelper adds connection testing, raw SQL, transactions and a log buffer, following the same style as AnointedAutomation.Repository.Mongo.

NuGet Downloads

Installation

dotnet add package AnointedAutomation.Repository.MySql

Quick start

Use GenericRepository<T> over your own DbContext, which defines the entity model:

using System.Collections.Generic;
using System.Threading.Tasks;
using AnointedAutomation.Repository.MySql;
using Microsoft.EntityFrameworkCore;

public class Customer
{
    public int Id { get; set; }
    public string Email { get; set; }
    public bool IsActive { get; set; }
}

public class ShopDbContext : DbContext
{
    public ShopDbContext(DbContextOptions<ShopDbContext> options) : base(options) { }
    public DbSet<Customer> Customers { get; set; }
}

public static class Example
{
    public static async Task RunAsync(string connectionString)
    {
        DbContextOptions<ShopDbContext> options = new DbContextOptionsBuilder<ShopDbContext>()
            .UseMySql(connectionString, ServerVersion.AutoDetect(connectionString))
            .Options;

        using ShopDbContext context = new ShopDbContext(options);
        IGenericRepository<Customer> customers = new GenericRepository<Customer>(context);

        Customer added = await customers.AddAsync(new Customer { Email = "john@example.com", IsActive = true });
        Customer byId = await customers.GetByIdAsync(added.Id);
        IEnumerable<Customer> active = await customers.FindAsync(c => c.IsActive);
        IEnumerable<Customer> firstPage = await customers.GetPagedAsync(c => c.IsActive, c => c.Email, descending: false, skip: 0, take: 20);
        int count = await customers.CountAsync();
        await customers.DeleteAsync(added.Id);
    }
}

API overview

All types are in the AnointedAutomation.Repository.MySql namespace.

IGenericRepository<T> / GenericRepository<T>

Constructor: GenericRepository<T>(DbContext context). Every write calls SaveChangesAsync immediately.

Area Methods
Read GetByIdAsync(object id), GetAllAsync(), FindAsync(predicate), FindSingleAsync(predicate), GetPagedAsync(predicate, orderBy, descending, skip, take), GetOrderedAsync(orderBy, descending, predicate = null), SelectAsync(predicate, selector), ExistsAsync(predicate), AnyAsync(predicate = null), CountAsync(predicate = null)
Write AddAsync(entity), AddRangeAsync(entities), UpdateAsync(entity), UpdateRangeAsync(entities), DeleteAsync(object id), DeleteAsync(T entity), DeleteRangeAsync(predicate)

IMySqlHelper / MySqlHelper

Member Description
DbContext Database, string ConnectionString, string DbName Settable state. new MySqlHelper(string connectionString) sets ConnectionString and creates Database via CreateDbContext.
CreateAsync, GetByIdAsync, GetAllAsync, GetFilteredAsync, FirstOrDefaultAsync, UpdateAsync, DeleteAsync, ExistsAsync, AnyAsync, CountAsync, GetPagedAsync, GetOrderedAsync, SelectAsync Generic (<T>) versions of the repository operations, run against Database.
ExecuteRawSqlAsync(string sql, params object[] parameters) Runs ExecuteSqlRawAsync; returns rows affected.
ExecuteInTransactionAsync(Func<Task> action) Commits if the action succeeds, rolls back and rethrows if it throws.
TestConnection() true if the database is reachable; logs success or the error.
GetTableNames() Table names from INFORMATION_SCHEMA for the current database, or null on error.
CreateDbContext(string connectionString) Creates a DynamicDbContext using Pomelo with ServerVersion.AutoDetect (this opens a connection to detect the server version).
static ConnectionStringBuilder(server, database, username, password, int port = 3306) Builds Server=...;Port=...;Database=...;User=...;Password=...;.
static MySqlHelperConnector(IMySqlHelper, string connectionString) Creates the context, sets the connection string and tests the connection.
static GetLogs(), static ClearLogs(), events LogAdded, LogCleared Static LogMessage buffer.

Other types

Type Description
IMySqlHelperFactory / MySqlHelperFactory Create(string connectionString) returns one cached IMySqlHelper per connection string; Remove(connectionString) and ClearCache() evict.
DynamicDbContext The empty DbContext created by MySqlHelper.CreateDbContext.

Using MySqlHelper with your own model

DynamicDbContext has no entity types configured, so the helper's generic CRUD methods only work once Database is a context that maps your entities. Assign your own context:

using AnointedAutomation.Repository.MySql;
using Microsoft.EntityFrameworkCore;

public static class HelperSetup
{
    public static IMySqlHelper Create(DbContext appContext, string connectionString)
    {
        IMySqlHelper helper = new MySqlHelper
        {
            Database = appContext,
            ConnectionString = connectionString
        };
        return helper;
    }
}

With your context in place, TestConnection, GetTableNames, ExecuteRawSqlAsync, ExecuteInTransactionAsync and the generic CRUD methods all run against it.

Dependency injection

GenericRepository<T> takes a DbContext, so expose your context under that base type:

using AnointedAutomation.Repository.MySql;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;

public static class MySqlSetup
{
    public static IServiceCollection AddShopData(this IServiceCollection services, string connectionString)
    {
        services.AddDbContext<ShopDbContext>(o =>
            o.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString)));
        services.AddScoped<DbContext>(sp => sp.GetRequiredService<ShopDbContext>());
        services.AddScoped(typeof(IGenericRepository<>), typeof(GenericRepository<>));
        return services;
    }
}

AddDbContext comes from Microsoft.EntityFrameworkCore; the IServiceCollection extension style assumes an ASP.NET Core or Generic Host app.

License

MIT. See LICENSE.

Support This Project

This library is free and open source. The best way to support the work is to shop with us:

Found a bug or have a request? Open an issue at https://github.com/AnointedAutomation/AnointedAutomation/issues.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.7 89 10/26/2026
1.0.6 105 10/1/2026
1.0.5 110 10/1/2026
1.0.4 100 9/25/2026
1.0.3 142 8/23/2026
1.0.2 124 7/26/2026
1.0.1 136 5/30/2026
1.0.0 153 3/15/2026
0.0.2 139 3/11/2026
0.0.1 145 2/19/2026