SyZero.SqlSugar 10.0.0

dotnet add package SyZero.SqlSugar --version 10.0.0
                    
NuGet\Install-Package SyZero.SqlSugar -Version 10.0.0
                    
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="10.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SyZero.SqlSugar" Version="10.0.0" />
                    
Directory.Packages.props
<PackageReference Include="SyZero.SqlSugar" />
                    
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 SyZero.SqlSugar --version 10.0.0
                    
#r "nuget: SyZero.SqlSugar, 10.0.0"
                    
#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@10.0.0
                    
#: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=10.0.0
                    
Install as a Cake Addin
#tool nuget:?package=SyZero.SqlSugar&version=10.0.0
                    
Install as a Cake Tool

SyZero.SqlSugar

SyZero 框架的 SqlSugar ORM 集成模块。

安装

dotnet add package SyZero.SqlSugar

配置

在 appsettings.json 中配置数据库连接:

{
  "ConnectionString": {
    "Type": "MySql",
    "Master": "Server=localhost;Database=MyDb;User=root;Password=123456;",
    "Slave": [
      {
        "ConnectionString": "Server=localhost;Database=MyDbRead;User=root;Password=123456;",
        "HitRate": 10
      }
    ]
  }
}

注册

使用默认 SyZeroDbContext

var builder = WebApplication.CreateBuilder(args);

builder.AddSyZero();
builder.Services.AddSyZeroSqlSugar(builder.Configuration);

var app = builder.Build();
app.UseSyZero();
app.InitTables();
app.Run();

使用自定义 DbContext

using Microsoft.Extensions.Logging;
using SqlSugar;
using SyZero.SqlSugar.DbContext;

public class MyDbContext : SyZeroDbContext
{
    public MyDbContext(ConnectionConfig config, ILoggerFactory loggerFactory)
        : base(config, loggerFactory)
    {
    }
}

builder.Services.AddSyZeroSqlSugar<MyDbContext>();

使用示例

using SyZero.Domain.Repository;

public class UserService
{
    private readonly IRepository<User> _userRepository;
    private readonly IUnitOfWork _unitOfWork;

    public UserService(IRepository<User> userRepository, IUnitOfWork unitOfWork)
    {
        _userRepository = userRepository;
        _unitOfWork = unitOfWork;
    }

    public Task<User> CreateUserAsync(User user)
    {
        return _userRepository.AddAsync(user);
    }

    public IQueryable<User> GetActiveUsers()
    {
        return _userRepository.GetList(x => x.IsActive);
    }

    public Task ExecuteInTransactionAsync(Func<Task> action)
    {
        return _unitOfWork.ExecuteInTransactionAsync(action);
    }
}

说明

  • ISyZeroDbContext、IRepository<>、IUnitOfWork 默认按 Scoped 注册。
  • 同一请求作用域内,仓储和工作单元会共享同一个 DbContext 实例。
  • GetList / GetPaged 返回可继续组合的查询对象,不会先把数据全部加载到内存。
  • SQL 日志默认通过 ILogger 输出原始 SQL 和参数。
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 (2)

Showing the top 2 NuGet packages that depend on SyZero.SqlSugar:

Package Downloads
SyZero.Example2.Core

Package Description

SyZero.Example1.Core

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
10.0.0 95 10/23/2026
9.1.0 82 10/22/2026
1.2.0 79 10/22/2026
1.2.0-dev.1 53 10/22/2026
1.1.9 152 4/19/2026
1.1.9-dev.2 87 4/19/2026
1.1.9-dev.1 80 4/17/2026
1.1.8 135 4/17/2026
1.1.6 135 4/17/2026
1.1.6-dev.1 80 4/17/2026
1.1.5 152 4/13/2026
1.1.5-dev.3 94 4/13/2026
1.1.5-dev.2 114 2/11/2026
1.1.5-dev.1 102 1/29/2026
1.1.4 181 1/2/2026
1.1.4-dev.2 107 1/2/2026
1.1.4-dev.1 98 12/30/2025
1.1.3 168 12/30/2025
1.1.3-dev.6 108 12/30/2025
1.0.0 232 4/17/2026
Loading failed