.net 简单的数据库操作框架
2026/6/8 0:45:07 网站建设 项目流程

介绍

SqlSugar是一款 老牌 .NET数据库操作框架,由果糖大数据科技团队维护和更新 ,Github star数仅次于EF 和 Dapper

优点: 简单易用、功能齐全、高性能、轻量级、服务齐全、有专业技术支持一天18小时服务

支持数据库:MySql、SqlServer、Sqlite、Oracle 、 postgresql、达梦、人大金仓

框架新功能

最新稳定版本5.0.2.8 ,发布后1个月时间NUGET下载量达到5000的版本,用户使用也相当满意

而在稳定版本的基础上又布了5.0.2.9版本

加入3大新功能

1. 配置查询

解决了大量字典表和简单就为取一个name 就要写联表的问题,让单表查询解决一切

2.多租户+仓储+自动分配

3.行转列

1、配置查询

解决了大量字典表和简单就为取一个name 就要写联表的问题,让单表查询解决一切

字典表我相信大家都全用到,他们可以方便的存储性别、学历、岗位等 一串数据 并进行TypeId进行区分

1.1 创建测试数据

创建一个字典实体

1

2

3

4

5

6

publicclassDataDictionary

{

publicstringCode {get;set; }

publicstringName {get;set; }

publicstringType {get;set; }

}

创建字典表并向里面插入测试数据

1

2

3

4

5

6

7

8

9

10

11

vardb = GetInstance();

List<DataDictionary> datas =newList<DataDictionary>();

datas.Add(newDataDictionary() { Code="1", Name="男",Type="sex"});

datas.Add(newDataDictionary() { Code ="2", Name ="女", Type ="sex"});

datas.Add(newDataDictionary() { Code ="1", Name ="南通市", Type ="city"});

datas.Add(newDataDictionary() { Code ="2", Name ="苏州市", Type ="city"});

datas.Add(newDataDictionary() { Code ="1", Name ="江苏省", Type ="province"});

datas.Add(newDataDictionary() { Code ="2", Name ="湖南省", Type ="province"});

db.CodeFirst.InitTables<DataDictionary>();//这样就能根据实体建表了

db.Insertable(datas).ExecuteCommand();//这样就能把数据插进数据库了<br>

在建一个Person表

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

publicclassPerson

{

//数据库字段

[SqlSugar.SugarColumn(IsPrimaryKey =true,IsIdentity =true)]

publicintId {get;set; }

publicstringName {get;set; }

publicintSexId {get;set; }

publicintCityId {get;set; }

publicintProvinceId {get;set; }

//非数据库字段

[SqlSugar.SugarColumn(IsIgnore =true)]

publicstringSexName {get;set; }

[SqlSugar.SugarColumn(IsIgnore =true)]

publicstringCityName {get;set; }

[SqlSugar.SugarColumn(IsIgnore =true)]

publicstringProviceName {get;set; }

}

1.2 传统字典联表实现缺点

如果我们要将Person中的非数据字典查询出来那么我们就需要写有2种实现方式

1.连表或者子查询 (缺点 写起来很浪费时间)

2.将字典存到内存,通过内存赋值 (缺点 字典表超过1000条以上性能很差 ,并且不能排序,或者LIKE)

下面介绍通过SqlSugar的配置查询解决上2面个难题

1.3 配置表简化字典联表

配置字典表

1

2

3

4

5

6

7

8

9

if(!db.ConfigQuery.Any())

{

vartypes= db.Queryable<DataDictionary>().Select(it => it.Type).Distinct().ToList();

foreach(vartypeintypes)

{

db.ConfigQuery.SetTable<DataDictionary>(it => it.Code, it => it.Name, type, it => it.Type == type);

}

//如果其中Code都是唯一值可以按 1.4中的用法使用循环都不要

}

配置完我们查询就会很方便了

1

2

3

4

5

6

7

8

9

varres=db.Queryable<Person>().Select(it =>newPerson()

{

Id=it.Id.SelectAll(),

SexName=it.SexId.GetConfigValue<DataDictionary>("sex"),

ProvinceName = it.ProvinceId.GetConfigValue<DataDictionary>("province"),

CityName = it.CityId.GetConfigValue<DataDictionary>("city"),

}).ToList();

//也支持支持写在Where或者Orderby

1.4 简单联表查询也可以配置

1

2

3

4

5

6

db.ConfigQuery.SetTable<Order>(it => it.Id, it => it.Name);//配置Order<br>

varlist3 = db.Queryable<OrderItem>().Select(it =>newOrderItem

{

ItemId = it.ItemId.SelectAll(),

OrderName = it.OrderId.GetConfigValue<Order>()//查询的时候直接用

}).ToList();

总结:配置表查询的方式可以大大降低重复联表问题,并且配置好后基本就不要写JOIN了

2、多租户+仓储+自动分配

SqlSugar多租户是通过ConfigId进行识别连接哪个库,新版本添加了实体配置ConfigId

1

2

3

4

5

6

7

8

9

10

11

[TenantAttribute("1")]

publicclassC1Table

{

publicstringId {get;set; }

}

[TenantAttribute("2")]

publicclassC2Table

{

publicstringId {get;set; }

}

下面我们仓储就可以通过实体配置自动识别是连接哪个库

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

publicclassRepository<T> : SimpleClient<T>whereT :class,new()

{

publicRepository(ISqlSugarClient context =null) :base(context)//注意这里要有默认值等于null

{

if(context ==null)

{

vardb =newSqlSugarClient(newList<ConnectionConfig> {

newConnectionConfig()

{

ConfigId="1",

DbType = SqlSugar.DbType.SqlServer,

IsAutoCloseConnection =true,

ConnectionString = Config.ConnectionString

},

newConnectionConfig()

{

ConfigId="2",

DbType = SqlSugar.DbType.SqlServer,

IsAutoCloseConnection =true,

ConnectionString = Config.ConnectionString2

}

});

base.Context = db;

varconfigId =typeof(T).GetCustomAttribute<TenantAttribute>().configId;

db.ChangeDatabase(configId);

}

}

/// <summary>

/// 扩展方法,自带方法不能满足的时候可以添加新方法

/// </summary>

/// <returns></returns>

publicList<T> CommQuery(stringsql)

{

//base.Context.Queryable<T>().ToList();可以拿到SqlSugarClient 做复杂操作

returnbase.Context.Queryable<T>().Where(sql).ToList();

}

}

新版本还添加了切换仓储功能

1

2

3

4

5

6

7

8

9

10

11

12

publicclassC1Service : Repository<C1Table>

{

publicvoidTest()

{

base.AsTenant().BeginTran();

base.GetList();//调用内部仓储方法

base.ChangeRepository<Repository<C2Table>>().GetList();//调用外部仓储

base.AsTenant().CommitTran();

}

}

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询