private void MakeFile(string sourceFile, string dirPath, string newFile, TempResult temp) { string file = FileHelper.ReadFile(sourceFile); file = file.Replace("%Namespace%", temp.Namespace); file = file.Replace("%TableName%", temp.TableName); file = file.Replace("%TableDesc%", temp.TableDesc); file = file.Replace("%ColumnEntity%", temp.ColumnEntity.TrimEnd()); file = file.Replace("%IMustHaveTenant%", temp.HasTenant ? ", IMustHaveTenant" : ""); FileHelper.WriteFile(newFile, dirPath, file); }
private void button5_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(textSQL.Text)) { button4_Click(null, null); } try { var columnEntity = new StringBuilder(); foreach (var c in _table.Columns) { columnEntity.AppendLine("\t\t/// <summary>"); columnEntity.AppendLine($"\t\t/// { c.ColumnDisplayName + (Global.IsRemarkAsAnnotation ? " " + c.ColumnRemark : "")}"); columnEntity.AppendLine("\t\t/// </summary>"); string typeName = c.GetColumnType(out int?typelen); //columnEntity.AppendLine("\t\t" + $@"[Column(""{c.ColumnName}"")]"); if (!string.IsNullOrEmpty(c.ColumnDisplayName)) { columnEntity.AppendLine("\t\t" + $@"[DisplayName(""{c.ColumnDisplayName}"")]"); } if (!c.AllowNull) { if (typeName == "string" && typeName == "byte[]" && typeName == "Guid") { columnEntity.AppendLine("\t\t" + @"[Required]"); } } if (typelen.HasValue) { columnEntity.AppendLine("\t\t" + $@"[StringLength({typelen.Value})]"); } if (typeName.Equals("decimal")) { c.GetDecimalPrecision(out int?precision, out int?scale); if (precision == null || scale == null || (precision.Value == 18 && scale.Value == 2)) { columnEntity.AppendLine("\t\t" + @"[DecimalPrecision]"); } else { columnEntity.AppendLine("\t\t" + $@"[DecimalPrecision({precision}, {scale})]"); } } if (c.IsPk) { columnEntity.AppendLine("\t\t" + @"[DatabaseGenerated(DatabaseGeneratedOption.None)]"); columnEntity.AppendLine("\t\t" + @"public override long Id { get; set; }"); } else { columnEntity.AppendLine("\t\t" + $@"public {typeName} {c.ColumnName} " + "{ get; set; }"); } columnEntity.AppendLine(); } var temp = new TempResult() { Namespace = textNamespace.Text, Module = textModule.Text, TableName = textClassEN.Text, TableDesc = textClassZH.Text, ColumnEntity = columnEntity.ToString(), HasTenant = _table.Columns.Any(p => p.ColumnName.Equals("TenantId", StringComparison.OrdinalIgnoreCase)) }; string currentDir = Directory.GetCurrentDirectory(); string patch1 = currentDir + "\\Code"; //生成Entity MakeFile(currentDir + "\\Template\\Entity.txt", patch1 + "\\Entity\\" + temp.Module, patch1 + "\\Entity\\" + temp.Module + "\\" + _table.TableName + ".cs", temp); System.Diagnostics.Process.Start(patch1 + "\\Entity\\" + temp.Module); ////生成DAL //MakeFile(patch + "\\Temp\\DAL.txt", patch1 + "\\DAL\\" + NameSpance, patch1 + "\\Kiloway.DAL\\" + NameSpance + "\\" + TableName + "DAL.cs", temp); ////生成Controller //MakeFile(patch + "\\Temp\\Controller.txt", patch1 + "\\Web\\Areas\\" + NameSpance + "\\Controllers", patch1 + "\\Web\\Areas\\" + NameSpance + "\\Controllers\\" + TableName + "Controller.cs", temp); ////生成View //MakeFile(patch + "\\Temp\\Form.txt", patch1 + "\\Web\\Areas\\" + NameSpance + "\\Views\\" + TableName, patch1 + "\\Web\\Areas\\" + NameSpance + "\\Views\\" + TableName + "\\Form.cshtml", temp); //MakeFile(patch + "\\Temp\\Index.txt", patch1 + "\\Web\\Areas\\" + NameSpance + "\\Views\\" + TableName, patch1 + "\\Web\\Areas\\" + NameSpance + "\\Views\\" + TableName + "\\Index.cshtml", temp); } catch (Exception ex) { MessageBox.Show(ex.Message); } }