private IFreeSql CreateDb(FreeSql.DataType dbType, DbInfo currentDbOption) { var master = currentDbOption.ConnectionStrings?.FirstOrDefault(e => e.UseType == DbUseType.Master); if (master == null) { throw new ArgumentNullException($"请设置租户 {Tenant.Code} 的主库连接字符串"); } var slaveConnectionStrings = currentDbOption.ConnectionStrings?.Where(e => e.UseType == DbUseType.Slave).Select(e => e.ConnectionString).ToArray(); var freeSqlBuilder = new FreeSql.FreeSqlBuilder() .UseConnectionString(dbType, master.ConnectionString); if (slaveConnectionStrings?.Length > 0) { freeSqlBuilder = freeSqlBuilder.UseSlave(slaveConnectionStrings); } if (_env.IsDevelopment()) { freeSqlBuilder = freeSqlBuilder.UseAutoSyncStructure(true); //自动同步实体结构【开发环境必备】 } var fsql = freeSqlBuilder.Build(); fsql.Aop.ConfigEntityProperty += ConfigEntityProperty; fsql.Aop.CurdBefore += CurdBefore; fsql.Aop.AuditValue += AuditValue; //fsql.Aop.SyncStructureAfter += SyncStructureAfter; DataFilterAsync(fsql); return(fsql); }
/// <summary> /// 获取数据库 /// </summary> /// <param name="dbType"></param> /// <param name="connectionStr"></param> /// <returns></returns> public IFreeSql GetDb(string connectionStr) { DbOption dbConfig = JsonConvert.DeserializeObject <DbOption>(connectionStr); var freeSqlBuilder = new FreeSql.FreeSqlBuilder() .UseConnectionString(dbConfig.DbType, dbConfig.ConnectionString) .UseAutoSyncStructure(false); var fsql = freeSqlBuilder.Build(); fsql.Aop.CurdBefore += CurdBefore; return(fsql); }