-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SqlSugar 多个 dbcontext 要怎么注册 实现 #32
Comments
根据 SqlSugar 官方文档简单修改
// 1. 添加 BaseDbContext 基类
namespace DbContext
{
public class BaseDbContext
{
private readonly ILogger<BaseDbContext> _logger;
public BaseDbContext(IOptionsSnapshot<SugarDbOptions> namedOptionsAccessor,
ILoggerFactory loggerFactory
)
{
_logger = loggerFactory.CreateLogger<BaseDbContext>();
var options = namedOptionsAccessor.Get(this.GetType().Name);
string connectionString = options.ConnectionString;
string dbType = options.DbType;
Db = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = connectionString,
DbType = (DbType)Enum.Parse(typeof(DbType), dbType),//DbType.SqlServer,
InitKeyType = InitKeyType.Attribute,//从特性读取主键和自增列信息
IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样我就不多解释了
});
//调式代码 用来打印SQL
Db.Aop.OnLogExecuting = (sql, pars) =>
{
_logger.LogInformation(sql + "\r\n" +
Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
};
}
public BaseDbContext(string connectionString, string dbType)
{
this.GetType();
Db = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = connectionString,
DbType = (DbType)Enum.Parse(typeof(DbType), dbType),//DbType.SqlServer,
InitKeyType = InitKeyType.Attribute,//从特性读取主键和自增列信息
IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样我就不多解释了
});
//调式代码 用来打印SQL
Db.Aop.OnLogExecuting = (sql, pars) =>
{
_logger.LogInformation(sql + "\r\n" +
Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
};
}
//注意:不能写成静态的
public SqlSugarClient Db;//用来处理事务多表查询和复杂的操作
}
} // 2. 添加 DbSet 数据集类
namespace DbContext
{
//可以直接用SimpleClient也可以扩展一个自个的类
//推荐直接用 SimpleClient
//为了照顾需要扩展的朋友,我们就来扩展一个SimpleClient,取名叫DbSet
public class DbSet<T> : SimpleClient<T> where T : class, new()
{
public DbSet(SqlSugarClient context) : base(context)
{
}
//SimpleClient中的方法满足不了你,你可以扩展自已的方法
public List<T> GetByIds(dynamic[] ids)
{
return Context.Queryable<T>().In(ids).ToList(); ;
}
}
} // 3. 添加 SugarDbOptions 配置类
namespace DbContext
{
public class SugarDbOptions
{
public string ConnectionString { get; set; } //连接字符串
public string DbType { get; set; } //数据库类型
}
} // 4. 根据项目需要,添加 DbContext01,DbContext02,...,继承于 BaseDbContext
namespace DbContext
{
public class DbContext01 : BaseDbContext
{
public DbContext01(IOptionsSnapshot<SugarDbOptions> namedOptionsAccessor,ILoggerFactory loggerFactory):base(namedOptionsAccessor, loggerFactory){}
// DbSet数据表
public DbSet<表1> 表1 { get { return new DbSet<表1>(Db); } }
public DbSet<表2> 表2 { get { return new DbSet<表2>(Db); } }
public DbSet<表3> 表3 { get { return new DbSet<表3>(Db); } }
}
} // 5. 添加扩展方法 ServiceCollectionExtensions.AddSugarDbContext
namespace Extensions
{
public static IServiceCollection AddSugarDbContext<T>(this IServiceCollection services, Action<SugarDbOptions> setupAction) where T : class
{
services.AddScoped<T>();
string name = typeof(T).Name;
var options = services.AddOptions<SugarDbOptions>(name);
services.Configure<SugarDbOptions>(name, setupAction);
return services;
}
}
} // 6. 在 ConfigureServices 添加 DbContext 服务注入 ConfigureServices
services.AddSugarDbContext<DbContext01>(options =>
{
options.ConnectionString = Configuration["DbContext:DbContext01:ConnectionString"];
options.DbType = Configuration["DbContext:DbContext01:DbType"];
}); //对应配置文件
"DbContext": {
"DbContext01": {
"Enabled": true,
"ConnectionString": "Server=.;Database=Db01;User ID=sa;Password=123;",
"DbType": "SqlServer"
}
}
|
你这个是配置多个上下文,每个上下文对应一个db库,如果使用的话,再去切换吧。 他问是好像是同时操作两个db,就是你说的,A租户过来自动使用A数据库连接字符串,B操作B。 |
services.AddScoped(provider =>
private readonly Func<string, SqlSugarClient> _serviceAccessor; 这是另外一个大佬提供的方案,可以学习一下 |
我这边实现了多库操作,但是不是多个上下文的。 |
在日常的使用当中,有需求要连接多个数据库来操作。这时候要怎么去处理这个dbcontext呢?
后期有安排这方面的东西吗?
The text was updated successfully, but these errors were encountered: