protected void TestDb() { var systemDb = TenantItem.DbOptions.Where(x => x.Key.ToLower() == "system").FirstOrDefault(); if (systemDb == null) { _ = MessageBox.AlertAsync("找不到System数据库配置"); return; } try { var dbType = (FreeSql.DataType)Enum.Parse(typeof(FreeSql.DataType), systemDb.DbType); var connStr = systemDb.ConnectionStrings.Where(x => x.UseType == DbUseType.Master).First().ConnectionString; IFreeSql fsql = new FreeSql.FreeSqlBuilder() .UseConnectionString(dbType, connStr) .Build(); // 这儿验证 连接是否成功 //var a = fsql.DbFirst.GetDatabases(); //Fsql.Ado.MasterPool.Get().Value DbConnection dbConnection = fsql.Ado.MasterPool.Get().Value; fsql.Dispose(); _ = MessageBox.AlertAsync("连接成功"); } catch (Exception ex) { _ = MessageBox.AlertAsync(ex.Message); } finally{ } }
public async Task <IResponseEntity> CreateDb(DbCreateItem req) { try { var dbType = (FreeSql.DataType)Enum.Parse(typeof(FreeSql.DataType), req.DbType); var connStr = req.ConnectionString; IFreeSql fsql = new FreeSql.FreeSqlBuilder() .UseConnectionString(dbType, connStr) .Build(); Console.WriteLine("\r\n开始建库"); await fsql.Ado.ExecuteNonQueryAsync(req.CreateDbCommand); fsql.Dispose(); Console.WriteLine("建库完成\r\n"); return(ResponseEntity.Ok("创建成功")); } catch (Exception ex) { Console.WriteLine($"建库失败.\n{ex.Message}\r\n"); return(ResponseEntity.Error(ex.Message)); } }
public IResponseEntity TestDbConnect(DbConnectItem req) { try { var dbType = (FreeSql.DataType)Enum.Parse(typeof(FreeSql.DataType), req.DbType); var connStr = req.ConnectionString; IFreeSql fsql = new FreeSql.FreeSqlBuilder() .UseConnectionString(dbType, connStr) .Build(); // 这儿验证 连接是否成功 //var a = fsql.DbFirst.GetDatabases(); //Fsql.Ado.MasterPool.Get().Value DbConnection dbConnection = fsql.Ado.MasterPool.Get().Value; fsql.Dispose(); return(ResponseEntity.Ok("连接成功")); } catch (Exception ex) { return(ResponseEntity.Error(ex.Message)); } }
protected void Submit() { if (!configForm.IsValid()) { return; } var systemDb = TenantItem.DbOptions.Where(x => x.Key.ToLower() == "system").FirstOrDefault(); if (systemDb == null) { _ = MessageBox.AlertAsync("找不到System数据库配置"); return; } try { Logs.Clear(); Logs.Add("保存配置 开始"); if (!WriteTenantSettings()) { _ = MessageBox.AlertAsync("配置保存失败"); return; } Logs.Add("保存配置 结束"); Logs.Add("创建数据连接对象 开始"); var dbType = (FreeSql.DataType)Enum.Parse(typeof(FreeSql.DataType), systemDb.DbType); var connStr = systemDb.ConnectionStrings.Where(x => x.UseType == DbUseType.Master).First().ConnectionString; IFreeSql fsql = new FreeSql.FreeSqlBuilder() .UseConnectionString(dbType, connStr) .Build(); DbConnection dbConnection = fsql.Ado.MasterPool.Get().Value; // 这儿验证 连接是否成功,这句代码可以不要,如果连接字符不变正确,为了提早发现(报异常) fsql.Aop.AuditValue += SyncDataAuditValue; Logs.Add("创建数据连接对象 结束"); Logs.Add("同步数据库结构 开始"); if (dbType == DataType.Oracle) { fsql.CodeFirst.IsSyncStructureToUpper = true; } fsql.CodeFirst.SyncStructure(new Type[] { typeof(SysApiEntity), // API typeof(SysConditionEntity), // 数据条件 相关功能暂未实现,表结构已设计 typeof(SysDictionaryDetailEntity), // 数据字典明细,相关功能暂未实现,表结构已设计 typeof(SysDictionaryEntity), // 数据字典主表,相关功能暂未实现,表结构已设计 typeof(SysGroupEntity), // 数据归属组 typeof(SysLoginLogEntity), // 登录日志 typeof(SysOperationLogEntity), // API访问日志 typeof(SysPermissionEntity), // 权限岗 typeof(SysResourceEntity), // 资源 typeof(SysRoleEntity), // 角色 typeof(SysRPermissionConditionEntity), // 权限岗 与 数据条件关系表,功能未实现 typeof(SysRPermissionGroupEntity), // 权限岗 与 数据组关系表 typeof(SysRPermissionRoleEntity), // 权限岗 与 角色关系表 typeof(SysRResourceApiEntity), // 资源 与 API关系表 typeof(SysRRoleResourceEntity), // 角色 与 资源关系表 typeof(SysSettingEntity), // 系统设置表,相关功能暂未实现,表结构已设计 typeof(SysUserEntity) // 用户表 }); Logs.Add("同步数据库结构 结束"); Logs.Add("种子数据初始化 开始"); using (var uow = fsql.CreateUnitOfWork()) using (var tran = uow.GetOrBeginTransaction()) { SeedDataEntity data = (new SeedData()).GetSeedData(); InitDtData(fsql, data.SysApiEntities.ToArray(), tran).Wait(); InitDtData(fsql, data.SysConditionEntities.ToArray(), tran).Wait(); InitDtData(fsql, data.SysDictionaryDetailEntities.ToArray(), tran).Wait(); InitDtData(fsql, data.SysDictionaryEntities.ToArray(), tran).Wait(); InitDtData(fsql, data.SysGroupEntities.ToArray(), tran).Wait(); InitDtData(fsql, data.SysPermissionEntities.ToArray(), tran).Wait(); InitDtData(fsql, data.SysResourceEntities.ToArray(), tran).Wait(); InitDtData(fsql, data.SysRoleEntities.ToArray(), tran).Wait(); InitDtData(fsql, data.SysRPermissionConditionEntities.ToArray(), tran).Wait(); InitDtData(fsql, data.SysRPermissionGroupEntities.ToArray(), tran).Wait(); InitDtData(fsql, data.SysRPermissionRoleEntities.ToArray(), tran).Wait(); InitDtData(fsql, data.SysRResourceApiEntities.ToArray(), tran).Wait(); InitDtData(fsql, data.SysRRoleResourceEntities.ToArray(), tran).Wait(); InitDtData(fsql, data.SysSettingEntities.ToArray(), tran).Wait(); InitDtData(fsql, data.SysUserEntities.ToArray(), tran).Wait(); uow.Commit(); } Logs.Add("种子数据初始化 结束"); fsql.Dispose(); // _ = MessageBox.AlertAsync("初始化成功"); } catch (Exception ex) { _ = MessageBox.AlertAsync(ex.Message); } }