public async Task <IResponseEntity> InitDb(DbConnectItem req) { StringBuilder sb = new StringBuilder(); sb.Append("<ul>"); sb.Append("<li>创建数据连接对象 开始</li>"); var dbType = (FreeSql.DataType)Enum.Parse(typeof(FreeSql.DataType), req.DbType); var connStr = req.ConnectionString; IFreeSql fsql = new FreeSqlBuilder() .UseConnectionString(dbType, connStr) .UseAutoSyncStructure(true) //自动同步实体结构【开发环境必备】 .Build(); DbConnection dbConnection = fsql.Ado.MasterPool.Get().Value; // 这儿验证 连接是否成功,这句代码可以不要,如果连接字符不变正确,为了提早发现(报异常) fsql.Aop.AuditValue += SyncDataAuditValue; sb.Append("<li>创建数据连接对象 结束</li>"); sb.Append("<li>创建数据库结构及初始化数据 开始</li>"); using (var uow = fsql.CreateUnitOfWork()) using (var tran = uow.GetOrBeginTransaction()) { SeedDataEntity data = (new SeedData()).GetSeedData(); sb.Append("<ul>"); await InitDtData(fsql, data.SysApiEntities.ToArray(), tran, sb); await InitDtData(fsql, data.SysConditionEntities.ToArray(), tran, sb); await InitDtData(fsql, data.SysDictionaryEntryEntities.ToArray(), tran, sb); await InitDtData(fsql, data.SysDictionaryHeaderEntities.ToArray(), tran, sb); await InitDtData(fsql, data.SysConfigEntities.ToArray(), tran, sb); await InitDtData(fsql, data.SysGroupEntities.ToArray(), tran, sb); await InitDtData(fsql, data.SysPermissionEntities.ToArray(), tran, sb); await InitDtData(fsql, data.SysResourceEntities.ToArray(), tran, sb); await InitDtData(fsql, data.SysRoleEntities.ToArray(), tran, sb); await InitDtData(fsql, data.SysRPermissionConditionEntities.ToArray(), tran, sb); await InitDtData(fsql, data.SysRPermissionRoleEntities.ToArray(), tran, sb); await InitDtData(fsql, data.SysRResourceApiEntities.ToArray(), tran, sb); await InitDtData(fsql, data.SysRRoleResourceEntities.ToArray(), tran, sb); await InitDtData(fsql, data.SysSettingEntities.ToArray(), tran, sb); await InitDtData(fsql, data.SysUserEntities.ToArray(), tran, sb); await InitDtData(fsql, data.SysLoginLogEntities?.ToArray(), tran, sb); await InitDtData(fsql, data.SysOperationLogEntities?.ToArray(), tran, sb); sb.Append("</ul>"); uow.Commit(); } sb.Append("<li>创建数据库结构及初始化数据 结束</li>"); sb.Append("</ul>"); fsql.Dispose(); return(ResponseEntity.Ok("初始化成功", msg: sb.ToString())); }
public IResponseEntity GenerateSeedData(DbConnectItem req) { var dbType = (FreeSql.DataType)Enum.Parse(typeof(FreeSql.DataType), req.DbType); var connStr = req.ConnectionString; IFreeSql fsql = new FreeSqlBuilder() .UseConnectionString(dbType, connStr) .Build(); var seedData = new SeedData(); seedData.GenerateSeedData(fsql); return(ResponseEntity.Ok("种子数据生成成功")); }
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)); } }