AnointedAutomation.Repository.MySql
1.0.6
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
<PackageReference Include="AnointedAutomation.Repository.MySql" Version="1.0.6" />
<PackageVersion Include="AnointedAutomation.Repository.MySql" Version="1.0.6" />
<PackageReference Include="AnointedAutomation.Repository.MySql" />
paket add AnointedAutomation.Repository.MySql --version 1.0.6
#r "nuget: AnointedAutomation.Repository.MySql, 1.0.6"
#:package AnointedAutomation.Repository.MySql@1.0.6
#addin nuget:?package=AnointedAutomation.Repository.MySql&version=1.0.6
#tool nuget:?package=AnointedAutomation.Repository.MySql&version=1.0.6
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.
Installation
dotnet add package AnointedAutomation.Repository.MySql
- Target framework:
net10.0 - Dependencies:
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.
Related packages
- AnointedAutomation.Logging: the
LogMessagetype used for the log buffer. - AnointedAutomation.Repository.Mongo: the MongoDB counterpart.
- AnointedAutomation.Objects: plain models you can map with EF Core.
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:
- Christian items: https://store.anointed.company
- Pets: https://www.animaldander.com
- Everything else: https://www.mart.club
Found a bug or have a request? Open an issue at https://github.com/AnointedAutomation/AnointedAutomation/issues.
| Product | Versions 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. |
-
net10.0
- AnointedAutomation.Logging (>= 1.0.5)
- Microsoft.EntityFrameworkCore (>= 10.0.12)
- Pomelo.EntityFrameworkCore.MySql (>= 9.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.