码迷,mamicode.com
首页 > 数据库 > 详细

.Net Core Api-SqlSugar的简单使用

时间:2020-06-21 00:41:39      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:图片   连接字符串   iss   class   connect   context   ext   mic   shard   

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     }

以上,仅用于学习和总结

.Net Core Api-SqlSugar的简单使用

标签:图片   连接字符串   iss   class   connect   context   ext   mic   shard   

原文地址:https://www.cnblogs.com/ywkcode/p/13170842.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!