JCode.ORMX.DbProvider.MySQL
1.0.3
dotnet add package JCode.ORMX.DbProvider.MySQL --version 1.0.3
NuGet\Install-Package JCode.ORMX.DbProvider.MySQL -Version 1.0.3
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="JCode.ORMX.DbProvider.MySQL" Version="1.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="JCode.ORMX.DbProvider.MySQL" Version="1.0.3" />
<PackageReference Include="JCode.ORMX.DbProvider.MySQL" />
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 JCode.ORMX.DbProvider.MySQL --version 1.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: JCode.ORMX.DbProvider.MySQL, 1.0.3"
#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 JCode.ORMX.DbProvider.MySQL@1.0.3
#: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=JCode.ORMX.DbProvider.MySQL&version=1.0.3
#tool nuget:?package=JCode.ORMX.DbProvider.MySQL&version=1.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
JCode.ORMX.DbProvider.MySQL
MySQL 数据库提供程序,用于 JCode.ORMX 框架。该提供程序使您能够使用 MySQL 作为 JCode.ORMX 的后端数据库。
功能特性
- 完整的 MySQL 支持:支持 MySQL 的所有基本操作
- 类型安全:使用强类型实体类,避免运行时错误
- LINQ 表达式树:使用 LINQ 表达式进行查询,编译时类型检查
- 表管理:自动创建和管理 MySQL 表
- 事务支持:支持 MySQL 事务操作
- 存储过程:支持 MySQL 存储过程调用
- 批量操作:支持批量插入、更新、删除
- 参数化查询:自动参数化查询,防止 SQL 注入
- 连接池:内置连接池管理,提高性能
安装
NuGet 包
Install-Package JCode.ORMX.DbProvider.MySQL
依赖项
- MySql.Data (8.x 或更高版本)
- JCode.ORMX (1.0.0 或更高版本)
快速开始
1. 定义实体类
[Table("Users")]
public class User
{
[Column(IsPrimaryKey = true, IsIdentity = true)]
public int Id { get; set; }
[Column]
public string Name { get; set; }
[Column]
public string Email { get; set; }
[Column]
public DateTime CreatedAt { get; set; }
}
2. 初始化 MySQL 提供程序
// 使用连接字符串
var provider = new MySQLProvider("Server=localhost;Database=MyDb;User Id=root;Password=your_password;");
// 或使用连接字符串构建器
var builder = new MySqlConnectionStringBuilder
{
Server = "localhost",
Database = "MyDb",
UserID = "root",
Password = "your_password",
Port = 3306,
SslMode = MySqlSslMode.None
};
var provider = new MySQLProvider(builder.ToString());
3. 创建表
var tableManager = provider.GetTableManager();
tableManager.Create(typeof(User));
4. 基本操作
var table = tableManager.Table<User>();
// 插入
var user = new User
{
Name = "John",
Email = "john@example.com",
CreatedAt = DateTime.Now
};
table.Insert(user);
// 查询
var users = table.Find()
.Where(u => u.Name == "John")
.OrderBy(u => u.Id)
.Limit(10)
.GetList();
// 更新
table.Update()
.Set("Email", "newemail@example.com")
.Where(u => u.Id == 1)
.Execute();
// 删除
table.Delete()
.Where(u => u.Id == 1)
.Execute();
高级功能
事务操作
using (var transaction = provider.BeginTransaction())
{
try
{
var table = transaction.Table<User>();
table.Insert(user1);
table.Insert(user2);
transaction.Commit();
}
catch
{
transaction.Rollback();
throw;
}
}
聚合查询
var result = table.Aggregate()
.GroupBy("Department")
.Count("EmployeeCount", "Id")
.Avg("AvgSalary", "Salary")
.Sum("TotalSalary", "Salary")
.GetList();
连接查询
var result = table.Find()
.InnerJoin<Order>((u, o) => u.Id == o.UserId)
.Where(u => u.Name == "John")
.GetList();
批量操作
// 批量插入
var users = new List<User> { /* ... */ };
table.InsertAll(users);
// 批量更新
table.UpdateAll(users);
// 批量删除
table.DeleteAll(users);
配置选项
连接字符串选项
var connectionString = "Server=localhost;Database=MyDb;User Id=root;Password=your_password;" +
"Port=3306;" +
"SslMode=None;" +
"AllowUserVariables=True;" +
"AllowPublicKeyRetrieval=True;" +
"ConnectionTimeout=30;" +
"DefaultCommandTimeout=30;" +
"Pooling=true;" +
"MinimumPoolSize=5;" +
"MaximumPoolSize=100;" +
"ConnectionLifeTime=300";
var provider = new MySQLProvider(connectionString);
高级配置
var builder = new MySqlConnectionStringBuilder
{
Server = "localhost",
Database = "MyDb",
UserID = "root",
Password = "your_password",
Port = 3306,
SslMode = MySqlSslMode.None,
AllowUserVariables = true,
AllowPublicKeyRetrieval = true,
ConnectionTimeout = 30,
DefaultCommandTimeout = 30,
Pooling = true,
MinimumPoolSize = 5,
MaximumPoolSize = 100,
ConnectionLifeTime = 300
};
var provider = new MySQLProvider(builder.ToString());
类型映射
MySQL 提供程序支持以下 .NET 类型到 MySQL 类型的映射:
| .NET 类型 | MySQL 类型 |
|---|---|
| int | INT |
| long | BIGINT |
| short | SMALLINT |
| byte | TINYINT |
| bool | BIT |
| decimal | DECIMAL |
| double | DOUBLE |
| float | FLOAT |
| string | VARCHAR |
| DateTime | DATETIME |
| DateTimeOffset | TIMESTAMP |
| Guid | CHAR(36) |
| byte[] | VARBINARY |
| enum | INT |
性能建议
- 使用索引:为常用查询字段创建索引
- 批量操作:使用 InsertAll、UpdateAll、DeleteAll 进行批量操作
- 查询优化:只查询需要的字段,使用 LIMIT 限制结果集
- 连接池:合理配置连接池大小
- 事务范围:尽量缩小事务范围,减少锁持有时间
- 避免 N+1 查询:使用连接查询代替多次查询
故障排除
常见问题
Q: 连接超时 A: 增加 ConnectionTimeout 和 DefaultCommandTimeout 参数的值
Q: 字符集问题
A: 在连接字符串中添加 CharSet=utf8mb4
Q: SSL 连接错误
A: 设置 SslMode=None 或配置正确的 SSL 证书
Q: 查询性能慢 A: 使用 EXPLAIN 分析查询计划,添加适当的索引
Q: 批量操作失败
A: 检查 max_allowed_packet 配置,增加其值以支持大批量操作
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. 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. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- JCode.ORMX (>= 1.0.3)
- MySqlConnector (>= 2.5.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.