ABP集成SqlSugar

SqlSugar是一款老牌 .NET 开源多库架构ORM框架,由果糖大数据科技团队维护和更新 ,生态圈丰富 ,开箱即用最易上手。

在Startup.ConfigureServices中注册SqlSugarClient

services.AddSingleton(s =>
{
	SqlSugarScope sqlSugar = new SqlSugarScope(new ConnectionConfig()
	{
		DbType = SqlSugar.DbType.SqlServer,
		ConnectionString = "Server=.\\SQL16; Database=MaikeABPStudy; Uid=sa; Pwd=123456;",
		IsAutoCloseConnection = true,
	},
   db =>
   {
	   //单例参数配置,所有上下文生效
	   db.Aop.OnLogExecuting = (sql, pars) =>
	   {
		   //获取作IOC作用域对象
		   //var appServive = s.GetService();
		   //var obj = appServive?.HttpContext?.RequestServices.GetService();
		   Console.WriteLine("sqlsugar:AOP");
	   };
   });
	return sqlSugar;
});

在core层添加接口

public interface ISqlSugarRepository : ISimpleClient where T : class, new()
{
    ISugarQueryable Queryable();
}

在EF层添加仓储的实现

/// 
/// 数据仓库类
/// 
/// 
public class SqlSugarRepository : SimpleClient, ISqlSugarRepository where T : class, new()
{
    public ITenant itenant = null;//多租户事务

    public SqlSugarRepository(ISqlSugarClient context = null) : base(context)
    {
        //Context = context;
    }
	
	
    public ISugarQueryable Queryable()
    {
        return Context.Queryable();
    }
}

在应用层注入使用

private ISqlSugarRepository _sqlsugarRepository;

//使用
var obj = _sqlsugarRepository.Queryable().First() ;

你可能感兴趣的:(ABP使用入门教程,sqlsugar,ABP)