SqlSugar是一款老牌 .NET 开源多库架构ORM框架,由果糖大数据科技团队维护和更新 ,生态圈丰富 ,开箱即用最易上手。
在Startup.ConfigureServices中注册SqlSugarClient和SqlSugarRepository
//注册上下文:AOP里面可以获取IOC对象,如果有现成框架比如Furion可以不写这一行
services.AddHttpContextAccessor();
//注册SqlSugar
services.AddSingleton<ISqlSugarClient>(s =>
{
SqlSugarScope sqlSugar = new SqlSugarScope(new ConnectionConfig()
{
DbType = SqlSugar.DbType.SqlServer,
ConnectionString = _appConfiguration.GetConnectionString("Default"),//"Server=.\\SQL16; Database=MaikeABPStudy; Uid=sa; Pwd=123456;",
IsAutoCloseConnection = true,
},
db =>
{
//自动过滤软删除
db.QueryFilter.AddTableFilter<ISoftDelete>(it => it.IsDeleted == false);
//自动过滤租户
db.QueryFilter.AddTableFilter<IMustHaveTenant>(it =>
it.TenantId == IocManager.Instance.IocContainer.Resolve<IAbpSession>().TenantId);
db.Aop.DataExecuting = (_, entityInfo) =>
{
switch (entityInfo.OperationType)
{
//执行insert时
case DataFilterType.InsertByObject:
//自动设置主键: 雪花id,单号
if (entityInfo.EntityColumnInfo.IsPrimarykey)
{
//var obj = (IEntity)entityInfo.EntityValue;
//obj.Id = YitIdHelper.NextId();
}
//自动设置时间和操作人
if (entityInfo.EntityValue is ICreationAudited)
{
var session = IocManager.Instance.IocContainer.Resolve<IAbpSession>();
var obj = (ICreationAudited)entityInfo.EntityValue;
obj.CreationTime = DateTime.Now;
obj.CreatorUserId = session.UserId;
}
//自动设置租户
if (entityInfo.EntityValue is IMustHaveTenant)
{
var session = IocManager.Instance.IocContainer.Resolve<IAbpSession>();
var obj = (IMustHaveTenant)entityInfo.EntityValue;
obj.TenantId = session.TenantId.Value;
}
break;
case DataFilterType.UpdateByObject:
//自动设置时间和操作人
if (entityInfo.EntityValue is IModificationAudited)
{
var session = IocManager.Instance.IocContainer.Resolve<IAbpSession>();
var obj = (IModificationAudited)entityInfo.EntityValue;
obj.LastModificationTime = DateTime.Now;
obj.LastModifierUserId = session.UserId;
}
break;
case DataFilterType.DeleteByObject:
//自动设置时间和操作人
if (entityInfo.EntityValue is IDeletionAudited)
{
var session = IocManager.Instance.IocContainer.Resolve<IAbpSession>();
var obj = (IDeletionAudited)entityInfo.EntityValue;
obj.DeletionTime = DateTime.Now;
obj.DeleterUserId = session.UserId;
}
break;
}
};
db.Aop.OnLogExecuting = (sql, pars) =>
{
LogHelper.Logger.Fatal(UtilMethods.GetSqlString(db.CurrentConnectionConfig.DbType, sql, pars));
};
db.Aop.OnError = ex =>
{
if (ex.Parametres == null)
{
LogHelper.Logger.Fatal(ex.Sql);
}
else
{
LogHelper.Logger.Fatal(UtilMethods.GetSqlString(db.CurrentConnectionConfig.DbType, ex.Sql, (SugarParameter[])ex.Parametres));
}
};
});
return sqlSugar;
});
//注册SqlSugar仓储
services.AddScoped(typeof(ISqlSugarRepository<>), typeof(SqlSugarRepository<>));
在CORE层添加接口
public interface ISqlSugarRepository<T> : ISimpleClient<T> where T : class, new()
{
ISqlSugarClient Context { get; set; }
ISugarQueryable<T> Queryable { get; }
}
在EF层添加仓储的实现
/// <summary>
/// 基础仓储类
/// </summary>
public class SqlSugarRepository<T> : SimpleClient<T>, ISqlSugarRepository<T> where T : class, new()
{
public SqlSugarRepository(ISqlSugarClient context = null) : base(context)
{
}
public ISugarQueryable<T> Queryable
{
get
{
return Context.Queryable<T>();
}
}
}
在应用层注入使用
private ISqlSugarRepository<XXXEntity> _sqlsugarRepository;
//使用
var obj = _sqlsugarRepository.Queryable.First() ;