完整框架项目源码地址:https://download.csdn.***/download/yigu4011/87788956?spm=1001.2014.3001.5503
仓储(Respository)是对数据库访问的一个封装
解决方案新建Respository文件夹,新建类库Web.Core.IRepository,Web.Core.Repository
解决方案新建Services文件夹,新建类库Web.Core.IServices,Web.Core.Services
在类库Web.Core.Model下面新建Entity文件夹
SqlSugar是国人开发者开发的一款高性能、轻量级 ORM框架,官网SqlSugar ORM 5.X 官网 、文档、教程 - SqlSugar 5x - .***果糖网
Respository层和Model层引入SqlSugarCore
Repository层新建suger文件夹,
新建BaseDBConfig.cs 数据库连接字符串
public class BaseDBConfig
{
/// <summary>
/// 数据库连接字符串
/// </summary>
public static string ConnectionString { get; set; }
}
新建DBConext.cs帮助类
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原理一样我就不多解释了
});
//调式代码 用来打印SQL
Db.Aop.OnLogExecuting = (sql, pars) =>
{
Console.WriteLine(sql + "\r\n" +
Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
Console.WriteLine();
};
}
//注意:不能写成静态的
public SqlSugarClient Db;//用来处理事务多表查询和复杂的操作
public SimpleClient<T> CurrentDb { get { return new SimpleClient<T>(Db); } }//用来操作当前表的数据
}
在program.cs里面增加读取appsettings.json中保存的数据库连接字符串
//数据库配置
BaseDBConfig.ConnectionString = builder.Configuration.GetSection("Ap