public RepositoryWriteInfrastructure(IDbContextFactory _factory, IRepositoryInfrastructureBase <TDbContext> _infrastructureBase, RepositoryOptions <TDbContext> _unitOfWorkOptions, IServiceProvider _serviceProvider) { masterDbContext = _factory.GetMaster <TDbContext>(); infrastructureBase = _infrastructureBase; unitOfWorkOptions = _unitOfWorkOptions; serviceProvider = _serviceProvider; }
/// <summary> /// 切换主库的上下文 /// </summary> /// <returns></returns> public Task SwitchMasterDbContextAsync() { if (!IsMaster && unitOfWorkOptions.IsOpenMasterSlave) { slaveDbContext = factory.GetMaster <TDbContext>(); } IsMaster = true; return(Task.CompletedTask); }
public RepositoryReadInfrastructure(IDbContextFactory _factory, IRepositoryInfrastructureBase <TDbContext> _infrastructureBase, RepositoryOptions <TDbContext> _unitOfWorkOptions) { factory = _factory; infrastructureBase = _infrastructureBase; unitOfWorkOptions = _unitOfWorkOptions; //验证是否开启了事务 如果开启了事务 就获取主库的连接 //或者没有开启读写分离 直接使用 主库的数据 if (_unitOfWorkOptions.IsBeginTran || !_unitOfWorkOptions.IsOpenMasterSlave) { slaveDbContext = _factory.GetMaster <TDbContext>(); IsMaster = true; } else { slaveDbContext = _factory.GetSlave <TDbContext>(); _infrastructureBase.SwitchSlaveAsync(slaveDbContext).ConfigureAwait(false).GetAwaiter().GetResult(); } }