zoukankan      html  css  js  c++  java
  • .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     }

    以上,仅用于学习和总结

  • 相关阅读:
    SQLServerframework启动报异常:Module的类型初始值设定项引发异常
    在coding或者github建立个人站点域名绑定
    Github速度慢的解决方法
    jsoup爬取网站图片
    activeMQ类别和流程
    Session session = connection.createSession(paramA,paramB);参数解析
    Ehcache入门经典:第二篇ehcache.xml的参数
    Ehcache入门经典:第一篇
    处理高并发
    扩充次数和创建个数问题
  • 原文地址:https://www.cnblogs.com/ywkcode/p/13170842.html
Copyright © 2011-2022 走看看