SqlSugar.Attributes
1.0.11
dotnet add package SqlSugar.Attributes --version 1.0.11
NuGet\Install-Package SqlSugar.Attributes -Version 1.0.11
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="SqlSugar.Attributes" Version="1.0.11" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SqlSugar.Attributes" Version="1.0.11" />
<PackageReference Include="SqlSugar.Attributes" />
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 SqlSugar.Attributes --version 1.0.11
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: SqlSugar.Attributes, 1.0.11"
#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 SqlSugar.Attributes@1.0.11
#: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=SqlSugar.Attributes&version=1.0.11
#tool nuget:?package=SqlSugar.Attributes&version=1.0.11
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
🏗️ SqlSugar.Attributes
SqlSugar.Attributes基于SqlSugar和C#特性 配置dto模型,只需要在模型上配置表信息,即可自动拼接SQL,实现查询插入更新等功能,实现高效项目开发和迭代。
🚀Getting Started
⚓ 查询
使用SqlSugar.Attributes可以直接配置DTO模型,配置完成后,将会自动映射查询条件及结果。
普通查询
- 创建查询的查询条件类
[DbDefaultOrderBy("t_Name", DbSortWay.DESC)]
//[DbDefaultOrderBy("t.t_Name", DbSortWay.DESC)]
public class Search
{
//[DbTableAlias("t")] //表别名,多表关联使用,单表需要查询时指定表别名
[DbQueryField("t_Name")] //字段名
[DbQueryOperator(DbOperator.Like)] //操作符, Like查询
public string Name { get; set; }
//[DbTableAlias("t")] //表别名,多表关联使用,单表需要查询时指定表别名
[DbQueryField("t_CodeId")] //字段名
[DbQueryOperator(DbOperator.In)] //操作符,IN查询
public List<long> CodeIds { get; set; }
}
- 创建查询的结果类
public class Result
{
//[DbTableAlias("t")] //表别名,多表关联使用,单表需要查询时指定表别名
[DbQueryField("t_Id")] //字段名
public long Id { get; set; }
//[DbTableAlias("t")] //表别名,多表关联使用,单表需要查询时指定表别名
[DbQueryField("t_Name")] //字段名
public string Name { get; set; }
//子查询,如果指定了表别名,可以使用表别名,否则直接写表名
//[DbSubQuery("(SELECT name FROM Table_Name2 WHERE t.t_Id = Id)")]
[DbSubQuery("(SELECT name FROM Table_Name2 WHERE Table_Name.t_Id = Id)")]
public string OtherName { get; set; }
}
- 查询方法
public class Test
{
/// <summary>
/// SqlSugar dbcontext
/// </summary>
private readonly ISqlSugarClient _dbContext;
public async Task<List<Result>> Get(Search search)
{
return await _dbContext.Queryable<Table_Name>()
.Where(search) //查询条件
.OrderBy(search) //排序
.Select(new Result()) //查询结果
.ToListAsync();
}
}
注:在使用SqlSugar.Attribute时,依然可以使用SqlSugar的Where,GroupBy,OrderBy等方法。
- 正常查询SQL
SELECT
t_Id as Id,
t_Name as Name,
(SELECT name FROM Table_Name2 WHERE Table_Name.t_Id = Id) AS OtherName
FROM Table_Name
WHERE t_Name LIKE '%@Name%' AND t_CodeId IN (@CodeIds)
ORDER BY t_Name DESC;
- 指定表别名查询SQL
SELECT
t.t_Id as Id,
t.t_Name as Name,
(SELECT name FROM Table_Name2 WHERE t.t_Id = Id) AS OtherName
FROM Table_Name t
WHERE t.t_Name LIKE '%@Name%' AND t.t_CodeId IN (@CodeIds)
ORDER BY t.t_Name DESC;
分组查询
- 创建查询的查询条件类
[DbDefaultOrderBy("CountNums", DbSortWay.DESC)]
public class Search
{
[DbQueryField("t_GroupCode")] //字段名
[DbQueryOperator(DbOperator.Like)] //操作符, Like查询
public long GroupCode { get; set; }
[DbQueryField("t_GroupName")] //字段名
[DbQueryOperator(DbOperator.Like)] //操作符, Like查询
public string GroupName { get; set; }
}
- 创建查询的结果类
[DbHaving("CountNums > 0")]
public class Result
{
[DbQueryField("t_GroupCode")] //字段名
[DbGroupBy("t_GroupCode")] //分组
public long GroupCode { get; set; }
[DbQueryField("t_GroupName")] //字段名
[DbGroupBy("t_GroupName")] //分组
public string GroupName { get; set; }
[DbSubQuery("Count(t_CodeId)")]
public string CountNums { get; set; }
}
- 查询方法
public class Test
{
/// <summary>
/// SqlSugar dbcontext
/// </summary>
private readonly ISqlSugarClient _dbContext;
public async Task<List<Result>> Get(Search search)
{
return await _dbContext.Queryable<Table_Name>()
.Where(search) //查询条件
.OrderBy(search) //排序
.GroupBy(new Result) //分组
.Having(new Result) //分组条件
.Select(new Result()) //查询结果
.ToListAsync();
}
}
- 查询SQL
SELECT
t_GroupCode as GroupCode,
t_GroupName as t_GroupName,
(Count(t_CodeId)) AS CountNums
FROM Table_Name
GROUP BY t_GroupCode,t_GroupName
HAVING CountNums > 0
WHERE t_GroupCode LIKE '%@Name%' AND t_GroupName LIKE '%@GroupName%'
ORDER BY CountNums DESC;
⚓ 插入
- 配置DTO模型
public class AddModel
{
[DbOperationField("t_Name")]
public string Name { get; set; }
[DbOperationField("t_Address")]
public string Address { get; set; }
}
- 新增方法
public class Test
{
/// <summary>
/// SqlSugar dbcontext
/// </summary>
private readonly ISqlSugarClient _dbContext;
public async Task<int> Add(AddModel model)
{
return await _dbContext.Insertable<AddModel,Table_Name>(model)
.ExecuteCommandAsync();
}
}
注:ExecuteCommandAsync()方法为SqlSugar的方法,这意味着可以使用其他的SqlSugar方法。
⚓ 更新
- 配置DTO模型
public class EditModel
{
[DbOperationField("t_Id", true)] //true代表更新条件
public long? Id { get; set; }
[DbOperationField("t_Name")]
public string Name { get; set; }
[DbOperationField("t_Address")]
public string Address { get; set; }
}
- 更新方法
public class Test
{
/// <summary>
/// SqlSugar dbcontext
/// </summary>
private readonly ISqlSugarClient _dbContext;
public async Task<bool> Add(EditModel model)
{
return await _dbContext.Updateable<EditModel,Table_Name>(model)
.ExecuteCommandHasChangeAsync();
}
}
注:ExecuteCommandHasChangeAsync()方法为SqlSugar的方法,这意味着可以使用其他的SqlSugar方法。
⚓ 更多用法
更多用法请参阅FastAdminAPI项目,该项目对SqlSugar.Attributes进行了更多的封装,更易于上手。
| 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
- SqlSugarCoreNoDrive (>= 5.1.4.58)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.