private void RefreshModelText(UniversalGenerator uniGen) { try { var list = uniGen.GetColumns(ComboBox_Database.Text, CheckedListBox_DataTable.Text); TextBox_Generated.Text = ModelGenerator.Generate(new ModelSetting() { Using = TextBox_Using.Text.Trim(), Namespace = TextBox_NameSpace.Text.Trim(), TabSpace = NumBox_TabSpace.Value.ToInt(), AccessModifier = ComboBox_AccessModifier.Text.Trim(), Inherit = TextBox_Inherit.Text.Trim(), ModelName = CheckedListBox_DataTable.Text.Trim(), ModelSummary = $"Model: {CheckedListBox_DataTable.Text.Trim()}", UseSummary = CheckBox_UseSummary.Checked, EnableSQLSugarSupport = ToolStripMenuItem_EnableSQLSugarDefaultSupport.Checked, Columns = list, }); } catch (Exception ex) { #if DEBUG Console.WriteLine($@"{ex.Message} {ex.StackTrace}"); #endif Alert.Error(ex.Message); return; } }
private void GenerateFile() { if (SqlGeneratorSetting == null) { Alert.Error("请先连接数据库"); return; } if (string.IsNullOrWhiteSpace(ComboBox_Database.Text)) { Alert.Error("请先选择数据库并勾选需要生成的表单"); return; } if (CheckedListBox_DataTable.CheckedItems.Count <= 0) { Alert.Error("请先勾选需要生成的表单"); return; } if (string.IsNullOrWhiteSpace(TextBox_SaveLocation.Text) && !ChangeSavePath()) { Alert.Error("请选择生成位置"); return; } if (!Directory.Exists(TextBox_SaveLocation.Text)) { Directory.CreateDirectory(TextBox_SaveLocation.Text); } using (var loadingForm = new WaitForm("正在生成", CheckedListBox_DataTable.CheckedItems.Count)) { var database = ComboBox_Database.Text; var modelSetting = new ModelSetting() { Using = TextBox_Using.Text.Trim(), Namespace = TextBox_NameSpace.Text.Trim(), TabSpace = NumBox_TabSpace.Value.ToInt(), AccessModifier = ComboBox_AccessModifier.Text.Trim(), Inherit = TextBox_Inherit.Text.Trim(), UseSummary = CheckBox_UseSummary.Checked, EnableSQLSugarSupport = ToolStripMenuItem_EnableSQLSugarDefaultSupport.Checked }; var hasGenerateError = false; var thread = new Thread(() => { using (var uniGen = new UniversalGenerator(SqlGeneratorSetting)) { if (!uniGen.TryGetConnection(out var message)) { Alert.Error(message); loadingForm.WorkComplete(); return; } for (int i = 0, showcount = 1; i < CheckedListBox_DataTable.CheckedItems.Count; ++i, ++showcount) { try { var modelName = CheckedListBox_DataTable.CheckedItems[i].ToString(); loadingForm.RefreshState(showcount, $"正在生成:\r\n{modelName}.cs"); modelSetting.ModelName = modelName; modelSetting.ModelSummary = $"Model: {modelName}"; modelSetting.Columns = uniGen.GetColumns(database, modelName); var generated_text = ModelGenerator.Generate(modelSetting); using (var sw = new StreamWriter($"{TextBox_SaveLocation.Text}\\{modelName}.cs")) { sw.Write(generated_text); } } catch (Exception ex) { hasGenerateError = true; var logFilePath = $"{TextBox_SaveLocation.Text}\\error.log"; if (!File.Exists(logFilePath)) { var fs = File.Create(logFilePath); fs.Dispose(); } using (var sw = new StreamWriter(logFilePath, true)) { sw.WriteLine($@"=== Exception log {DateTime.Now:yyyy-MM-dd hh:mm:ss} BEGINS === Exception message: {ex.Message} Exception stack trace: {ex.StackTrace} === Exception log ENDS ==="); } } } } loadingForm.WorkComplete(); }) { IsBackground = true }; loadingForm.StartWork(thread); if (hasGenerateError) { Alert.Warning("生成已完成,但出现错误,错误报告已生成在与实体类相同的目录下"); } else { Alert.Info("生成已完成"); } } }