Muta.SqlSugar.DynamicTable 1.0.0.2

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

Muta.ApiExtensions.SqlSugar

Muta.ApiExtensions.SqlSugar 是基于 SqlSugarCore 的 动态 数据访问扩展库,提供以下能力:

  • QuestDB 动态表仓储(运行时建表、插入、动态条件分页、分组取最新)。

1. 安装与依赖

Install-Package Muta.SqlSugar.DynamicTable

主要依赖:

  • SqlSugarCore

2. 快速接入

2.1 appsettings.json

{
  "SqlSugar": [ 
    {
      "ConnectionString": "host=127.0.0.1;port=8812;username=admin;password=quest;database=qdb;ServerCompatibilityMode=NoTypeLoading;",
      "DbType": "QuestDB",
      "ConfigId": "QuestDB",
      "IsAutoCloseConnection": true
    }
  ]
}

2.2 Program.cs / Startup.cs

using SqlSugar;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();

// 1) 注册 SqlSugar 客户端(showNativeSql=true 时会输出原生 SQL)
builder.Services.AddMutaSqlSugarClient(builder.Configuration, showNativeSql: true);

// 2) 注册 SqlSugar 版 Swagger(含 dto 请求体裁剪)
builder.Services.AddMutaSwaggerDocumentation(
    title: "Demo API",
    version: "v1",
    xmlFileNames: new[] { "Demo.Api.xml" },
    useBearer: true).AddMutaSqlSugarSwaggerDoc();

// 3) 注册业务仓储 
builder.Services.AddScoped(typeof(IDynamicTableRepository<>), typeof(DynamicQuestDbTableRepository<>));

var app = builder.Build();

// 可选:应用启动后执行数据库初始化逻辑
app.UseMutaInitDb((db, config) =>
{
    // 示例:db.CodeFirst.InitTables(typeof(User));
});

app.MapControllers();
app.Run();

3.1 IDynamicTableRepository<TSplitBase> + DynamicQuestDbTableRepository<TSplitBase>

文件:

  • Repositories/Domain/IDynamicTableRepository.cs
  • Repositories/Infrastructure/DynamicQuestDbTableRepository.cs

用途:面向 QuestDB 的动态表操作扩展。

3.1.1 动态建表 CreateTable
var columns = new List<DynamicTableColumn>
{
    new("DeviceId", typeof(string), new SugarColumn { Length = 64 }),
    new("Value", typeof(double), null)
};

var result = dynamicRepo.CreateTable("DeviceMetric_202604", columns, "设备指标月表");
3.1.2 动态插入 InsertAsync
await dynamicRepo.InsertAsync("DeviceMetric_202604", new Dictionary<string, object>
{
    ["DeviceId"] = "D-001",
    ["Value"] = 12.34,
    ["Time"] = DateTime.UtcNow
});
3.1.3 动态分页查询 QueryPageAsync
var page = await dynamicRepo.QueryPageAsync(
    tableName: "DeviceMetric_202604",
    conditions: new List<DynamicQueryCondition>
    {
        new("DeviceId", "=", "D-001"),
        new("Value", ">", 10)
    },
    pageIndex: 1,
    pageSize: 20,
    orderByField: "Time",
    isAsc: false);
3.1.4 分组取最新 QueryLatestByGroupFieldSqlAsync
var latest = await dynamicRepo.QueryLatestByGroupFieldSqlAsync(
    tableName: "DeviceMetric_202604",
    groupField: "DeviceId",
    groupValues: new object[] { "D-001", "D-002" },
    timeField: "Time",
    isAsc: false);
3.1.5 取首条/取首条字段
var first = await dynamicRepo.QueryFirstAsync("DeviceMetric_202604", null, "Time", false);

double? firstValue = await dynamicRepo.QueryFirstFieldAsync<double>(
    tableName: "DeviceMetric_202604",
    targetField: "Value",
    orderByField: "Time",
    isAsc: false);

注意:当前实现固定使用 ConfigId = "QuestDB" 连接。


4. 常用模型说明

  • DynamicTableColumn:动态建表字段定义(字段名、类型、SugarColumn 配置)。
  • DynamicQueryCondition:动态查询条件(字段、操作符、值)。
  • DynamicPagedResult:动态分页结果(Total + Data)。

5. 最小可运行组合示例

builder.Services.AddControllers();

builder.Services.AddHttpClient("QuestDbRest", client =>
{
    client.Timeout = TimeSpan.FromSeconds(120);
});

var sqlSugarConfigs = builder.Configuration.GetSection("SqlSugar").Get<List<ConnectionConfig>>(); 
builder.Services.AddSingleton<ISqlSugarClient>(new SqlSugarScope(sqlSugarConfigs));

builder.Services.AddScoped(typeof(IRepositoryBase<,>), typeof(RepositoryBase<,>));

var app = builder.Build(); 

app.MapControllers();
app.Run();

6. 注意事项

  • QuestDB 动态仓储默认走 ConfigId = "QuestDB",请确保该连接存在。
  • repo.SetConfigId("QuestDB1"); 可切换到其他连接。
Product 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.

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.0.0.2 104 4/22/2026
1.0.0.1 103 4/22/2026