SyZero.SqlSugar
1.1.4-dev.1
This is a prerelease version of SyZero.SqlSugar.
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package SyZero.SqlSugar --version 1.1.4-dev.1
NuGet\Install-Package SyZero.SqlSugar -Version 1.1.4-dev.1
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="SyZero.SqlSugar" Version="1.1.4-dev.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SyZero.SqlSugar" Version="1.1.4-dev.1" />
<PackageReference Include="SyZero.SqlSugar" />
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 SyZero.SqlSugar --version 1.1.4-dev.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: SyZero.SqlSugar, 1.1.4-dev.1"
#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 SyZero.SqlSugar@1.1.4-dev.1
#: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=SyZero.SqlSugar&version=1.1.4-dev.1&prerelease
#tool nuget:?package=SyZero.SqlSugar&version=1.1.4-dev.1&prerelease
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
SyZero.SqlSugar
基于 SqlSugar ORM 的数据访问组件,提供仓储模式和工作单元支持。
📦 安装
dotnet add package SyZero.SqlSugar
✨ 特性
- 🚀 多数据库支持 - 支持 MySQL、SqlServer、PostgreSQL、Oracle、SQLite 等
- 🔄 读写分离 - 支持主从数据库配置
- 📦 仓储模式 - 内置泛型仓储,开箱即用
- 🔒 工作单元 - 支持事务管理
- 🏷️ 属性映射 - 支持标准 DataAnnotations 属性
- ⚡ 自动建表 - 支持 CodeFirst 自动初始化表结构
🚀 快速开始
1. 配置连接字符串
在 appsettings.json 中添加数据库配置:
{
"ConnectionOptions": {
"Type": 0,
"Master": "Server=localhost;Database=MyDb;User=root;Password=123456;",
"Slave": [
{
"HitRate": 10,
"ConnectionString": "Server=slave1;Database=MyDb;User=root;Password=123456;"
}
]
}
}
数据库类型 Type 对应值:
| 值 | 数据库 |
|---|--------|
| 0 | MySql |
| 1 | SqlServer |
| 2 | SQLite |
| 3 | Oracle |
| 4 | PostgreSQL |
2. 注册服务
// Program.cs
var builder = WebApplication.CreateBuilder(args);
// 方式一:使用默认 DbContext
builder.Services.AddSyZeroSqlSugar();
// 方式二:使用自定义 DbContext
builder.Services.AddSyZeroSqlSugar<MyDbContext>();
var app = builder.Build();
// 可选:自动初始化表结构
app.InitTables();
app.Run();
3. 定义实体
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using SyZero.Domain.Entities;
[Table("users")]
public class User : Entity
{
[Required]
[Column("user_name")]
public string UserName { get; set; }
[Column("email")]
public string Email { get; set; }
public int? Age { get; set; } // 可空类型自动设为 IsNullable
}
4. 使用仓储
public class UserService
{
private readonly IRepository<User> _userRepository;
public UserService(IRepository<User> userRepository)
{
_userRepository = userRepository;
}
public async Task<User> GetUserAsync(long id)
{
return await _userRepository.GetModelAsync(id);
}
public async Task<IQueryable<User>> GetActiveUsersAsync()
{
return await _userRepository.GetListAsync(u => u.IsActive);
}
public async Task<User> CreateUserAsync(User user)
{
return await _userRepository.AddAsync(user);
}
}
📖 API 说明
IRepository<TEntity> 接口
查询方法
| 方法 | 说明 |
|---|---|
GetModel(long id) |
根据 ID 获取实体 |
GetModel(Expression<Func<TEntity, bool>> where) |
根据条件获取单个实体 |
GetList() |
获取所有实体 |
GetList(Expression<Func<TEntity, bool>> where) |
根据条件获取实体列表 |
GetPaged(...) |
分页查询 |
Count(Expression<Func<TEntity, bool>> where) |
统计数量 |
新增方法
| 方法 | 说明 |
|---|---|
Add(TEntity entity) |
添加单个实体 |
AddList(IQueryable<TEntity> entities) |
批量添加实体 |
更新方法
| 方法 | 说明 |
|---|---|
Update(TEntity entity) |
更新单个实体 |
Update(IQueryable<TEntity> entities) |
批量更新实体 |
删除方法
| 方法 | 说明 |
|---|---|
Delete(long id) |
根据 ID 删除 |
Delete(Expression<Func<TEntity, bool>> where) |
根据条件删除 |
所有方法都有对应的异步版本(带
Async后缀)
IUnitOfWork 接口
| 方法 | 说明 |
|---|---|
BeginTransaction() |
开启事务 |
CommitTransaction() |
提交事务 |
RollbackTransaction() |
回滚事务 |
DisposeTransaction() |
释放事务 |
🔧 高级用法
自定义 DbContext
public class MyDbContext : SyZeroDbContext
{
public MyDbContext(ConnectionConfig config) : base(config)
{
// 自定义 AOP 处理
this.Context.Aop.OnLogExecuting = (sql, pars) =>
{
Console.WriteLine($"执行 SQL: {sql}");
};
}
}
使用工作单元(事务)
public class OrderService
{
private readonly IRepository<Order> _orderRepository;
private readonly IRepository<OrderItem> _orderItemRepository;
private readonly IUnitOfWork _unitOfWork;
public OrderService(
IRepository<Order> orderRepository,
IRepository<OrderItem> orderItemRepository,
IUnitOfWork unitOfWork)
{
_orderRepository = orderRepository;
_orderItemRepository = orderItemRepository;
_unitOfWork = unitOfWork;
}
public async Task CreateOrderAsync(Order order, List<OrderItem> items)
{
try
{
await _unitOfWork.BeginTransactionAsync();
await _orderRepository.AddAsync(order);
foreach (var item in items)
{
item.OrderId = order.Id;
await _orderItemRepository.AddAsync(item);
}
await _unitOfWork.CommitTransactionAsync();
}
catch
{
await _unitOfWork.RollbackTransactionAsync();
throw;
}
}
}
分页查询
public async Task<IQueryable<User>> GetUsersPagedAsync(int pageIndex, int pageSize)
{
// 按创建时间降序分页
return await _userRepository.GetPagedAsync(
pageIndex,
pageSize,
u => u.CreationTime,
isDesc: true
);
}
public async Task<IQueryable<User>> SearchUsersAsync(string keyword, int pageIndex, int pageSize)
{
// 带条件的分页查询
return await _userRepository.GetPagedAsync(
pageIndex,
pageSize,
u => u.CreationTime,
u => u.UserName.Contains(keyword),
isDesc: true
);
}
实体属性映射
支持标准的 System.ComponentModel.DataAnnotations 属性:
[Table("tb_products")] // 指定表名
public class Product : Entity
{
[Key] // 主键
public override long Id { get; set; }
[Required] // 非空(string 类型默认可空)
[Column("product_name")] // 指定列名
public string Name { get; set; }
[NotMapped] // 忽略映射
public string TempData { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] // 自增
public int OrderNo { get; set; }
}
📁 项目结构
SyZero.SqlSugar/
├── DbContext/
│ ├── ISyZeroDbContext.cs # DbContext 接口
│ └── SyZeroDbContext.cs # DbContext 实现
├── Repositories/
│ └── SqlSugarRepository.cs # 泛型仓储实现
├── UnitOfWork/
│ └── UnitOfWork.cs # 工作单元实现
└── SyZeroSqlSugarExtension.cs # 依赖注入扩展方法
⚠️ 注意事项
- 连接配置 - 确保
appsettings.json中的ConnectionOptions配置正确 - 实体基类 - 实体类需要继承
Entity或实现IEntity接口 - 表初始化 -
InitTables()会自动创建所有继承IEntity的实体对应的表 - 读写分离 - 配置
Slave节点后自动启用读写分离,读操作随机分配到从库
📄 许可证
MIT License - 详见 LICENSE
| 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 | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | 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.1
- SqlSugarCore (>= 5.1.4.211)
- SyZero (>= 1.1.4-dev.1)
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.1.5-dev.2 | 57 | 2/11/2026 |
| 1.1.5-dev.1 | 51 | 1/29/2026 |
| 1.1.4 | 115 | 1/2/2026 |
| 1.1.4-dev.2 | 60 | 1/2/2026 |
| 1.1.4-dev.1 | 53 | 12/30/2025 |
| 1.1.3 | 111 | 12/30/2025 |
| 1.1.3-dev.6 | 59 | 12/30/2025 |
| 1.1.3-dev.3 | 124 | 1/19/2024 |
| 1.1.3-dev.2 | 191 | 11/3/2023 |
| 1.1.3-dev.1 | 195 | 3/21/2023 |
| 1.1.2 | 423 | 3/15/2023 |
| 1.1.2-dev.108.29344 | 206 | 3/15/2023 |
| 1.1.2-dev.108.28054 | 204 | 3/15/2023 |
| 1.1.2-dev.108.27487 | 196 | 3/15/2023 |
| 1.1.1 | 358 | 3/15/2023 |
| 1.1.1-dev.108.14980 | 182 | 3/15/2023 |
| 1.1.1-dev.108.13289 | 187 | 3/15/2023 |
| 1.1.1-dev.107.27144 | 190 | 3/14/2023 |
| 1.1.0 | 353 | 3/14/2023 |
| 1.1.0-dev.107.26364 | 188 | 3/14/2023 |
Loading failed