![]() |
VOOZH | about |
dotnet add package HMENetCore.SqlSugarNoDrive --version 10.0.10
NuGet\Install-Package HMENetCore.SqlSugarNoDrive -Version 10.0.10
<PackageReference Include="HMENetCore.SqlSugarNoDrive" Version="10.0.10" />
<PackageVersion Include="HMENetCore.SqlSugarNoDrive" Version="10.0.10" />Directory.Packages.props
<PackageReference Include="HMENetCore.SqlSugarNoDrive" />Project file
paket add HMENetCore.SqlSugarNoDrive --version 10.0.10
#r "nuget: HMENetCore.SqlSugarNoDrive, 10.0.10"
#:package HMENetCore.SqlSugarNoDrive@10.0.10
#addin nuget:?package=HMENetCore.SqlSugarNoDrive&version=10.0.10Install as a Cake Addin
#tool nuget:?package=HMENetCore.SqlSugarNoDrive&version=10.0.10Install as a Cake Tool
HMENetCore.SqlSugarNoDrive 是基于SqlSugar( https://github.com/donet5/SqlSugar ) ORM的增强封装库,提供对SQL Server和MySQL数据库的便捷操作支持。采用分层架构设计(Context → Repository → Service),内置丰富的CRUD操作和高级查询功能。
✔ **多数据库支持**
✅ SQL Server 2008+
✅ MySQL 5.7+
✔ **企业级功能**
🔒 分布式事务支持
🔄 主从读写分离
💯 完善的批量操作
✔ **开发效率**
🚀 全异步API设计
📊 内置分页查询组件
🧩 开箱即用的仓储模式
dotnet add package HMENetCore.SqlSugarNoDrive
appsettings.json:{
"DataBaseConfig": {
"ConnectionString": "Server=.;Database=DemoDB;Uid=sa;Pwd=123456;",
"DbType": "SqlServer", // SqlServer/MySql
"AutoClose": true,
//读写分离配置
"Slaves": [
{
"ConnectionString": "Server=slave1;Database=DemoDB;Uid=sa;Pwd=123456;",
"HitRate": 10
}
]
}
}
builder.Services
.Configure<DataBaseConfig>(builder.Configuration.GetSection("DataBaseConfig"))
.AddSqlSugarSetup(builder.Configuration.Get<DataBaseConfig>()!);
// 或使用 Action 配置方式
builder.Services.AddSqlSugarSetup(options =>
{
options.ConnectionString = Configuration.GetConnectionString("DefaultConnection");
options.DbType = DbType.SqlServer; // 或 DbType.MySql
options.AutoClose = true;
options.DisableNvarchar = false;
// 其他配置...
});
[SugarTable("Users")]
public class User
{
[SugarColumn(IsPrimaryKey = true)]
public string Id { get; set; }
public string Name { get; set; }
[SugarColumn(ColumnName = "create_time")]
public DateTime CreateTime { get; set; }
}
public class UserRepository : BaseSqlServerRepository<User>
{
public UserRepository(IDbContext context) : base(context)
{
}
// 自定义查询方法
public async Task<List<User>> GetActiveUsersAsync()
{
return await Db.Queryable<User>()
.Where(u => u.CreateTime > DateTime.Now.AddMonths(-1))
.ToListAsync();
}
}
public class UserService : BaseSqlServerService<User>
{
public UserService(ISqlServerCRUD<User> repository) : base(repository)
{
}
public async Task UpdateUserNameAsync(string userId, string newName)
{
await Db.Updateable<User>()
.SetColumns(u => u.Name == newName)
.Where(u => u.Id == userId)
.ExecuteCommandAsync();
}
}
提供对 DbContext 的基本操作接口。
仓储层基类,实现了 ISqlServerCRUD 接口,提供对特定实体的 CRUD 操作。
服务层基类,封装了仓储层的操作,可以在此基础上添加业务逻辑。
// 基础分页
var page = await Db.QueryPageAsync<User>(
where: u => u.Name.Contains("张"),
pageIndex: 1,
pageSize: 20,
orderByFileds: "Id DESC");
// 复杂分页
var page = await Db.QueryPageAsync<User, UserVO>(
where: u => u.Age > 18,
select: u => new UserVO { Name = u.Name, Age = u.Age },
pageIndex: 1,
pageSize: 20,
orders: new Dictionary<Expression<Func<User, object>>, string> {
{ u => u.CreateTime, "DESC" },
{ u => u.Name, "ASC" }
});
// 自动事务
var result = await Db.UseTranAsync(async () =>
{
await userService.UpdateAsync(user);
await orderService.CreateAsync(newOrder);
});
// 手动事务
try
{
Db.Ado.BeginTran();
await userRepo.UpdateAsync(user);
await logRepo.InsertAsync(log);
Db.Ado.CommitTran();
}
catch
{
Db.Ado.RollbackTran();
throw;
}
// 批量插入(10万条数据约2秒)
var data = Generate100KUsers();
await Db.BulkCopyAsync(data);
// 批量更新
await Db.BulkUpdateAsync(updatedUsers);
// 智能合并(存在更新,不存在插入)
await Db.BulkStorageAsync(users);
// 租户过滤器
Db.QueryFilter.Add(new TableFilterItem<User>(
query => query.Where(u => u.TenantId == currentTenantId)));
public class OrderService
{
private readonly IUnitOfWork _uow;
private readonly IRepository<Order> _orderRepo;
private readonly IRepository<User> _userRepo;
public OrderService(
IUnitOfWork uow,
IRepository<Order> orderRepo,
IRepository<User> userRepo)
{
_uow = uow;
_orderRepo = orderRepo;
_userRepo = userRepo;
}
public async Task CreateOrder(OrderCreateDto dto)
{
try
{
_uow.BeginTran();
// 扣减用户余额
await _userRepo.UpdateAsync(
u => u.Id == dto.UserId,
u => u.Balance == u.Balance - dto.TotalAmount);
// 创建订单
var order = new Order { /* 初始化订单 */ };
await _orderRepo.InsertAsync(order);
_uow.CommitTran();
}
catch
{
_uow.RollbackTran();
throw;
}
}
}
public class Repository<T> : IRepository<T> where T : class, new()
{
protected readonly IUnitOfWork _uow;
public Repository(IUnitOfWork uow)
{
_uow = uow;
}
protected ISqlSugarClient Db => _uow.Db;
public async Task<int> InsertAsync(T entity)
{
return await Db.Insertable(entity).ExecuteCommandAsync();
}
// 其他CRUD方法...
}
// 自定义仓储示例
public interface ICustomOrderRepository : IRepository<Order>
{
Task<List<Order>> GetRecentOrdersAsync(int days);
}
public class CustomOrderRepository : Repository<Order>, ICustomOrderRepository
{
public CustomOrderRepository(IUnitOfWork uow) : base(uow) { }
public async Task<List<Order>> GetRecentOrdersAsync(int days)
{
return await Db.Queryable<Order>()
.Where(o => o.CreateTime >= DateTime.Now.AddDays(-days))
.ToListAsync();
}
}
public class TestUnitOfWorkService : BaseUnitOfWorkService<Test2>, ITestUnitOfWorkService
{
public TestUnitOfWorkService(IUnitOfWork unitOfWork) : base(unitOfWork)
{
}
public async Task<Test2> FirstAsync(Expression<Func<Test2, bool>> expression)
{
return await DbClient.Queryable<Test2>().FirstAsync(expression);
}
}
public interface ITestUnitOfWorkService : IBaseUnitOfWorkCRUD<Test2>
{
Task<Test2> FirstAsync(Expression<Func<Test2, bool>> expression);
}
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 is compatible. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 netstandard2.1 is compatible. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
This package is not used by any NuGet packages.
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 10.0.10 | 94 | 5/18/2026 |
| 10.0.9 | 149 | 3/17/2026 |
| 10.0.8 | 125 | 2/28/2026 |
| 10.0.7 | 123 | 2/24/2026 |
| 10.0.6 | 121 | 2/2/2026 |
| 10.0.5 | 128 | 1/14/2026 |
| 6.1.11 | 100 | 5/18/2026 |
| 6.1.10 | 146 | 3/17/2026 |
| 6.1.9 | 137 | 2/28/2026 |
| 6.1.8 | 123 | 2/24/2026 |
| 6.1.7 | 120 | 2/2/2026 |
| 6.1.6 | 125 | 1/14/2026 |
| 6.1.5 | 134 | 1/14/2026 |