private void btnGenerate_Click(object sender, EventArgs e) { List <TableInfo> list = this.dataGridView1.DataSource as List <TableInfo>; StringBuilder addMapperCode = new StringBuilder(200); foreach (var t in list) { ModelGenerator.GenerateModel(t); var modeClassName = ResolveString(t.TableName); var className = $"{modeClassName}Mapper"; var sb = GenerateClassCode(t, className, modeClassName); using (StreamWriter sw = new StreamWriter(new FileStream("../../codes/" + className + ".cs", FileMode.Create, FileAccess.Write))) { sw.WriteLine(sb.ToString()); } addMapperCode.AppendLine($"modelBuilder.Configurations.Add(new {className}());"); } using (StreamWriter sw = new StreamWriter(new FileStream("../../codes/" + "addMapperCode.txt", FileMode.Create, FileAccess.Write))) { sw.WriteLine(addMapperCode.ToString()); } MessageBox.Show("完成"); }
/// <summary> /// 点击“批量生成按钮” =》 批量生成 Model、DAL、数据库结构表 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void generate_button_Click(object sender, EventArgs e) { //1. 判断是否选择了表 if (Right_listBox.Items.Count <= 0) { MessageBox.Show("请选择要生成的表!"); return; } //2. 判断要生成的路径是否为空 string outputPath = path_textBox.Text.Trim(); if (outputPath.ToString() == "") { //如果是空值,则给一个默认路径 outputPath = "\\\\Mac\\Home\\Desktop"; path_textBox.Text = outputPath; } //3. 判断要生成的路径是否存在 if (!Directory.Exists(outputPath)) { try { //如果路径不存在,则自动生成路径 Directory.CreateDirectory(outputPath); } catch (Exception) { //出错 MessageBox.Show("路径不存在!请选择正确的生成目录!"); return; } } //4. 判断命名空间是否为空 if (namespace_textBox.Text.Trim().ToString() == "") { MessageBox.Show("请输入命名空间!"); return; } //5. 获取基本信息 string connStr = getConnstr(); string tablePrefix = table_prefix_textBox.Text.Trim(); string author = author_textBox.Text.Trim(); string ns = namespace_textBox.Text.Trim(); string dbName = DB_name_textBox.Text.Trim(); //6. 生成 Model ModelGenerator.GenerateModel(ns, author, outputPath, tablePrefix, connStr, Right_listBox.Items); //7. 生成 DAL DalGenerator.GenerateDAL(ns, author, outputPath, tablePrefix, connStr, Right_listBox.Items); //8. 生成数据库操作助手类 MSSQLHelperGenerator.GenerateMSSQLHelper(ns, outputPath, connStr); //9. 生成 数据库文档 SqlFileGenerator.GenerateSqlFile(outputPath, connStr, listRight, dbName); //10. 告知生成成功 string successMsg = "恭喜你!生成成功!"; MessageBox.Show(successMsg); //11. 打开生成后的目录 System.Diagnostics.Process.Start(outputPath); }