首页 > 数据库技术 > 详细

.Net Core Api-SqlSugar的简单使用

时间:2020-06-20 23:52:10      阅读:558      评论:0      收藏:0      [点我收藏+]

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的简单使用

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!