?完整框架項目源碼地址:https://download.csdn.net/download/yigu4011/87788956?spm=1001.2014.3001.5503
倉儲(Respository)是對數(shù)據(jù)庫訪問的一個封裝
解決方案新建Respository文件夾,新建類庫Web.Core.IRepository,Web.Core.Repository
解決方案新建Services文件夾,新建類庫Web.Core.IServices,Web.Core.Services
在類庫Web.Core.Model下面新建Entity文件夾
SqlSugar是國人開發(fā)者開發(fā)的一款高性能、輕量級 ORM框架,官網(wǎng)SqlSugar ORM 5.X 官網(wǎng) 、文檔、教程 - SqlSugar 5x - .NET果糖網(wǎng)
Respository層和Model層引入SqlSugarCore
?Repository層新建suger文件夾,
新建BaseDBConfig.cs 數(shù)據(jù)庫連接字符串
public class BaseDBConfig
{
/// <summary>
/// 數(shù)據(jù)庫連接字符串
/// </summary>
public static string ConnectionString { get; set; }
}
新建DBConext.cs幫助類文章來源:http://www.zghlxwxcb.cn/news/detail-799871.html
public class DbContext<T> where T : class, new()
{
public DbContext()
{
Db = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = BaseDBConfig.ConnectionString,
DbType = DbType.SqlServer,
InitKeyType = InitKeyType.Attribute,//從特性讀取主鍵和自增列信息
IsAutoCloseConnection = true,//開啟自動釋放模式和EF原理一樣我就不多解釋了
});
//調(diào)式代碼 用來打印SQL
Db.Aop.OnLogExecuting = (sql, pars) =>
{
Console.WriteLine(sql + "\r\n" +
Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
Console.WriteLine();
};
}
//注意:不能寫成靜態(tài)的
public SqlSugarClient Db;//用來處理事務(wù)多表查詢和復(fù)雜的操作
public SimpleClient<T> CurrentDb { get { return new SimpleClient<T>(Db); } }//用來操作當(dāng)前表的數(shù)據(jù)
}
?在program.cs里面增加讀取appsettings.json中保存的數(shù)據(jù)庫連接字符串文章來源地址http://www.zghlxwxcb.cn/news/detail-799871.html
//數(shù)據(jù)庫配置
BaseDBConfig.ConnectionString = builder.Configuration.GetSection("Ap
到了這里,關(guān)于.Net Core6.0 WebAPI項目框架搭建六:倉儲模式+導(dǎo)入SqlSuagr的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!