小白开学Asp.Net Core <一>

开篇中介绍了项目的搭建以及项目中所用到的第三方工具

本篇介绍SqlSugar和Dapper在本项目的实现

一、SqlSugar

  SqlSuagr的介绍就直接浏览官方网站,官网地址将在底部给出。

  

在本项目中我将ORM放在了基础设施层下的Common库中,在DotNet Core中引用SqlSugar时请引用 sqlSugarCore 版,我在此项目中引用的版本是:4.9.9.10

至于安装直接NuGet搜索安装

 1 using SqlSugar;
 2 using DbType = SqlSugar.DbType;
 3 
 4 namespace Aju.Carefree.Common.DataBaseCore
 5 {
 6     public abstract class DbFactory
 7     {
 8         public static SqlSugarClient DB => GetInstance();
 9         static SqlSugarClient GetInstance()
10         {
11             string connectionString = "Server=127.0.0.1;Database=DB_Area;Integrated Security=False;User ID=sa;Password=123456;";
12             var db = new SqlSugarClient(
13                 new ConnectionConfig
14                 {
15                     ConnectionString = connectionString,
16                     DbType = DbType.SqlServer,
17                     IsAutoCloseConnection = true,
18                     InitKeyType = InitKeyType.Attribute
19                 }
20             );
21             return db;
22         }
23     }
24 }

 二、Dapper

   Dapper的介绍就直接浏览官方网站,官网地址将在底部给出。

  

  NuGet安装

  本项目中使用的版本号是:1.60.1

 1 using System.Data;
 2 using System.Data.SqlClient;
 3 
 4 namespace Aju.Carefree.Common.DapperCore
 5 {
 6     public class DapperHelper
 7     {
 8         public static string DapperDbConnectionString { get; set; }
 9 
10         public static IDbConnection GetSqlConnection(string sqlConnectionString = null)
11         {
12             if (string.IsNullOrWhiteSpace(sqlConnectionString))
13             {
14                 sqlConnectionString = DapperDbConnectionString;
15             }
16             IDbConnection conn = new SqlConnection(sqlConnectionString);
17             conn.Open();
18             return conn;
19         }
20     }
21 }

本项目中用到的ORM官网地址:

  SqlSugar:http://www.codeisbug.com/

  Dapper:   https://dapper-tutorial.net/

本篇就到这里,下篇将介绍仓储层的实现

(本人坚信:学习是由浅到深的过程,先打基础)

  DotNet Core 的好处相信大家都已经了解了,就不再这里聊了!

  不喜勿喷!谢谢!

GitHub地址:

  https://github.com/AjuPrince/Aju.Carefree

猜你喜欢

转载自www.cnblogs.com/haoxiaozhang/p/10844855.html
今日推荐