/// <summary> /// 1.生成实体类【多个文件】 /// [模板文件]:domain/domain.txt /// [生成文件名]:ClassName /// [生成文件后缀名]:.cs /// </summary> private void CreateDomain() { string ClassName = "Admin"; string Table_Comment = "系统管理员"; string Template_Name, Unit_Template, Content, Content_New; string UnitColumnDefine; string Column_Name, Column_Type, Column_Comment, Column_Null; string Relation_ClassName, Relation_InstanceName, Relation_Table_Name; string Relation_Unit_Template = "", Relation_Table_Comment = "", Relation_UnitColumnDefine = "";//相关列数据对象定义 string OneHasMany_Table_Comment = "", OneHasMany_Unit_Template = "", OneHasMany_UnitColumnDefine = ""; foreach (string Table_Name in TableList) { //读取原文件内容到内存 Template_Name = @"AutoCode/Model/domain/domain.txt"; Content = UtilFile.ReadFile2String(Template_Name); ClassName = Table_Name; if (TableInfoList.ContainsKey(Table_Name)) { Table_Comment = TableInfoList[Table_Name]["Comment"]; string[] t_c = Table_Comment.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); if (t_c.Length > 1) { Table_Comment = ""; foreach (string Comment_Single_Line in t_c) { Table_Comment += " /// " + Comment_Single_Line + "\r\n"; } } else { Table_Comment = " /// " + Table_Comment; } Content_New = Content.Replace("{$ClassName}", ClassName); Content_New = Content_New.Replace("{$Table_Comment}", Table_Comment); Dictionary <string, Dictionary <string, string> > FieldInfo = FieldInfos[Table_Name]; UnitColumnDefine = ""; Relation_UnitColumnDefine = ""; OneHasMany_UnitColumnDefine = ""; foreach (KeyValuePair <String, Dictionary <string, string> > entry in FieldInfo) { Column_Name = entry.Key; Column_Comment = entry.Value["Comment"]; Column_Type = entry.Value["Type"]; string[] c_c = Column_Comment.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); if (c_c.Length > 1) { Column_Comment = ""; foreach (string Comment_Single_Line in c_c) { Column_Comment += " /// " + Comment_Single_Line + "\r\n"; } Column_Comment = Column_Comment.Substring(0, Column_Comment.Length - 2); } else { Column_Comment = " /// " + Column_Comment; } Unit_Template = @" /// <summary> {$Column_Comment} /// </summary> public {$Column_Type} {$Column_Name} { get; set; } "; bool IsNull = true; Column_Null = entry.Value["Null"]; if (Column_Null.Equals("否")) { IsNull = false; } Column_Type = ColumnTypeByDbDefine(Column_Type, Column_Name, IsNull); Unit_Template = Unit_Template.Replace("{$Column_Comment}", Column_Comment); Unit_Template = Unit_Template.Replace("{$Column_Type}", Column_Type); Unit_Template = Unit_Template.Replace("{$Column_Name}", Column_Name); UnitColumnDefine += Unit_Template; if (Column_Name.Contains("_ID")) { Relation_ClassName = Column_Name.Replace("_ID", ""); if (TableList.Contains(Relation_ClassName)) { //读取原文件内容到内存 Template_Name = @"AutoCode/Model/domain/httpdata.txt"; Content = UtilFile.ReadFile2String(Template_Name); Relation_InstanceName = UtilString.LcFirst(Relation_ClassName); Relation_Table_Name = Relation_ClassName; if (TableInfoList.ContainsKey(Relation_Table_Name)) { Relation_Table_Comment = TableInfoList[Relation_Table_Name]["Comment"]; t_c = Relation_Table_Comment.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); if (c_c.Length > 1) { Relation_Table_Comment = ""; foreach (string Comment_Single_Line in c_c) { Relation_Table_Comment += " /// " + Comment_Single_Line + "\r\n"; } Relation_Table_Comment = Relation_Table_Comment.Substring(0, Column_Comment.Length - 2); } else { Relation_Table_Comment = " /// " + Relation_Table_Comment; } //多对一关系需定义 Relation_Unit_Template = @" /// <summary> {$Relation_Table_Comment} /// </summary> public virtual {$Relation_ClassName} {$Relation_ClassName} { get; set; }"; Relation_Unit_Template = Relation_Unit_Template.Replace("{$Relation_ClassName}", Relation_ClassName); Relation_Unit_Template = Relation_Unit_Template.Replace("{$Relation_Table_Comment}", Relation_Table_Comment); Relation_UnitColumnDefine += Relation_Unit_Template; } } } } if (OneHasManyDefine.ContainsKey(Table_Name)) { List <string> lOneHasMany = OneHasManyDefine[Table_Name]; foreach (string OneHasMany_TableName in lOneHasMany) { OneHasMany_Table_Comment = TableInfoList[OneHasMany_TableName]["Comment"]; t_c = OneHasMany_Table_Comment.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); if (t_c.Length > 1) { OneHasMany_Table_Comment = ""; foreach (string Comment_Single_Line in t_c) { OneHasMany_Table_Comment += " /// " + Comment_Single_Line + "\r\n"; } OneHasMany_Table_Comment = OneHasMany_Table_Comment.Substring(0, OneHasMany_Table_Comment.Length - 2); } else { OneHasMany_Table_Comment = " /// " + OneHasMany_Table_Comment; } OneHasMany_Unit_Template = @" /// <summary> {$OneHasMany_Table_Comment} /// </summary> public virtual ICollection<{$OneHasMany_TableName}> {$OneHasMany_TableName} { get; set; } "; OneHasMany_Unit_Template = OneHasMany_Unit_Template.Replace("{$OneHasMany_Table_Comment}", OneHasMany_Table_Comment); OneHasMany_Unit_Template = OneHasMany_Unit_Template.Replace("{$OneHasMany_TableName}", OneHasMany_TableName); OneHasMany_UnitColumnDefine += OneHasMany_Unit_Template; } } UnitColumnDefine += Relation_UnitColumnDefine; UnitColumnDefine += OneHasMany_UnitColumnDefine; Content_New = Content_New.Replace("{$ColumnDefines}", UnitColumnDefine); //存入目标文件内容 UtilFile.WriteString2File(Save_Dir + ClassName + ".cs", Content_New); } } }
/// <summary> /// 1a.生成实体类分部类(显示属性) /// </summary> private void CreateDomainPartial() { string ClassName = "Admin"; string InstanceName = "admin"; string Table_Comment = "系统管理员"; string Template_Name, UnitTemplate, Content, Content_New; string Column_Name, Column_Comment, Column_Type, Column_Length; string Relation_ClassName, Relation_Column_Comment, Relation_Table_Name, Relation_Column_Name; string UnitColumnDefine; foreach (string Table_Name in TableList) { //读取原文件内容到内存 Template_Name = @"AutoCode/Model/domain/domain.txt"; Content = UtilFile.ReadFile2String(Template_Name); ClassName = Table_Name; if (TableInfoList.ContainsKey(Table_Name)) { Dictionary <string, Dictionary <string, string> > FieldInfo = FieldInfos[Table_Name]; UnitColumnDefine = ""; foreach (KeyValuePair <String, Dictionary <string, string> > entry in FieldInfo) { Column_Name = entry.Key; Column_Comment = entry.Value["Comment"]; Column_Type = entry.Value["Type"]; Column_Length = entry.Value["Length"]; string[] c_c = Column_Comment.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); if (c_c.Length >= 1) { Column_Comment = c_c[0]; } int iLength = UtilNumber.Parse(Column_Length); if (Column_Type.Equals("tinyint")) { Column_Comment = entry.Value["Comment"]; c_c = Column_Comment.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); if (c_c.Length > 1) { Column_Comment = ""; foreach (string Comment_Single_Line in c_c) { Column_Comment += " /// " + Comment_Single_Line + "\r\n"; } Column_Comment = Column_Comment.Substring(0, Column_Comment.Length - 2); UnitTemplate = @" /// <summary> {$Column_Comment} /// </summary> public String {$Column_Name}Show { get; set; }"; UnitTemplate = UnitTemplate.Replace("{$Column_Comment}", Column_Comment); UnitTemplate = UnitTemplate.Replace("{$Column_Name}", Column_Name); UnitColumnDefine += UnitTemplate; } } else if (Column_Name.Contains("_ID")) { Relation_ClassName = Column_Name.Replace("_ID", ""); if (TableList.Contains(Relation_ClassName) || (Relation_ClassName.ToUpper().Equals("PARENT"))) { if (Relation_ClassName.ToUpper().Equals("PARENT")) { Relation_Table_Name = Relation_ClassName; Relation_Column_Name = ""; Dictionary <string, Dictionary <string, string> > Relation_FieldInfo = FieldInfos[ClassName]; foreach (KeyValuePair <String, Dictionary <string, string> > relation_entry in Relation_FieldInfo) { Relation_Column_Name = relation_entry.Key; if (UtilString.Contains(relation_entry.Key.ToUpper(), "NAME", "TITLE", "URL")) { break; } } Column_Comment = Column_Comment.Replace("标识", ""); UnitTemplate = @" /// <summary> /// 显示{$Column_Comment} /// </summary> public String {$Relation_Column_Name}_Parent { get; set; } /// <summary> /// 显示{$Column_Comment}[全] /// </summary> public String {$ClassName}ShowAll { get; set; } "; UnitTemplate = UnitTemplate.Replace("{$Column_Comment}", Column_Comment); UnitTemplate = UnitTemplate.Replace("{$ClassName}", ClassName); UnitTemplate = UnitTemplate.Replace("{$Relation_Column_Name}", Relation_Column_Name); UnitColumnDefine += UnitTemplate; } else if (TableInfoList.ContainsKey(Relation_ClassName)) { Relation_Table_Name = Relation_ClassName; Dictionary <string, Dictionary <string, string> > Relation_FieldInfo = FieldInfos[Relation_Table_Name]; Relation_Column_Name = Column_Name; Relation_Column_Comment = Relation_Column_Name; foreach (KeyValuePair <String, Dictionary <string, string> > relation_entry in Relation_FieldInfo) { Relation_Column_Name = relation_entry.Key; if (UtilString.Contains(relation_entry.Key.ToUpper(), "NAME", "TITLE", "URL")) { Relation_Column_Comment = relation_entry.Value["Comment"]; break; } } UnitTemplate = @" /// <summary> /// {$Relation_Column_Comment} /// </summary> public String {$Relation_Column_Name} { get; set; }"; UnitTemplate = UnitTemplate.Replace("{$Relation_Column_Comment}", Relation_Column_Comment); UnitTemplate = UnitTemplate.Replace("{$Relation_Column_Name}", Relation_Column_Name); UnitColumnDefine += UnitTemplate; } } } else if (ColumnIsTextArea(Column_Name, Column_Type, iLength)) { UnitTemplate = @" /// <summary> /// {$Column_Comment} /// </summary> public String {$Column_Name}Show { get; set; }"; Column_Comment = entry.Value["Comment"]; c_c = Column_Comment.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); if (c_c.Length > 1) { Column_Comment = ""; foreach (string Comment_Single_Line in c_c) { Column_Comment += " /// " + Comment_Single_Line + "\r\n"; } Column_Comment = Column_Comment.Substring(0, Column_Comment.Length - 2); } UnitTemplate = UnitTemplate.Replace("{$Column_Comment}", Column_Comment); UnitTemplate = UnitTemplate.Replace("{$Column_Name}", Column_Name); UnitColumnDefine += UnitTemplate; } } if (OneHasManyDefine.ContainsKey(Table_Name)) { UnitTemplate = @" /// <summary> /// 辅助主键 ///【用于ExtJs Combo主键下拉框显示ID以和主表ID名称区别开来】 /// </summary> public String {$Table_Name}_ID { get; set; }"; UnitTemplate = UnitTemplate.Replace("{$Table_Name}", Table_Name); UnitColumnDefine += UnitTemplate; } if (!string.IsNullOrEmpty(UnitColumnDefine)) { Table_Comment = TableInfoList[Table_Name]["Comment"]; string[] t_c = Table_Comment.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); if (t_c.Length > 1) { Table_Comment = ""; foreach (string Comment_Single_Line in t_c) { Table_Comment += " /// " + Comment_Single_Line + "\r\n"; } } else { Table_Comment = " /// " + Table_Comment; } Content_New = Content.Replace("{$ClassName}", ClassName); Content_New = Content_New.Replace("{$Table_Comment}", Table_Comment); Content_New = Content_New.Replace("{$InstanceName}", InstanceName); Content_New = Content_New.Replace("{$ColumnDefines}", UnitColumnDefine); //存入目标文件内容 UtilFile.WriteString2File(Save_Dir + ClassName + ".cs", Content_New); } } } }