.Net Core Api-SqlSugar的简单使用

SqlSugar一款轻量级,高性能,支持.Net4.+和ASP.Net Core的一款ORM框架

使用如下:

1.引入Nuget包 SqlSugarCore

 

 

2.新建“SqlSugarBase.cs”
 1 public class SqlsugarBase
 2     {
 3       /// <summary>
 4       /// 连接字符串
 5       /// </summary>
 6     public string connectionString = string.Empty;
 7  
 8     /// <summary>
 9     ///
10     /// </summary>
11     public SqlSugarClient DB => GetInstance();
12 
13 
14     SqlSugarClient GetInstance()
15     {
16 
17 
18         var db = new SqlSugarClient(
19             new ConnectionConfig
20             {
21                 ConnectionString = connectionString,
22                 DbType = DbType.MySql,
23                 IsShardSameThread = true,
24                 IsAutoCloseConnection = true,
25                 InitKeyType = InitKeyType.Attribute
26             }
27         );
28         return db;
29     }

3.在Service层添加“FrameInformationService.cs”继承SqlSugarBase,给连接字符串赋值

 1  public class FrameInformationService:SqlsugarBase
 2     {
 3         public FrameInformationService(IConfiguration _configuration)
 4         {
 5             this.connectionString = _configuration.GetValue<string>("ConnectionStrings:CoreFrameContext");
 6         }
 7 
 8 
 9         public List<Frame_Information> GetList()
10         {
11             return DB.Queryable<Frame_Information>().ToList();
12         }
13      }

4.控制器中调用

 1  public class InfoController : ControllerBase
 2     {
 3         public readonly FrameInformationService _service;
 4  
 5         public InfoController(FrameInformationService service)
 6         {
 7             _service = service;
 8         }  
 9         
10         [HttpGet("GetInfoList")]
11         public ApiReturnValue GetInfoList(int ImgCount, int InfoCount)
12         {
13             ApiReturnValue apiReturnValue = new ApiReturnValue();
14             apiReturnValue.Result = _service.GetList();
15             return apiReturnValue;
16         }
17     }

以上,仅用于学习和总结

posted @ 2020-06-20 23:09  y_w_k  阅读(4626)  评论(0编辑  收藏  举报