void cbxTables_SelectedIndexChanged(object sender, EventArgs e) { tbxServerAddress_TextChanged(null, null); if (cbxTables.SelectedValue.GetType() != typeof(string)) { return; } try { string tablename = cbxTables.SelectedValue.ToString(); List <DbColumn> list = DbHelper.GetDbColumns(connection, Catalog, tablename, _confg.schema); string classname = tablename + "Entity"; string _namespace = txtnamespace.Text.Trim() + "." + tablename; dataGridView1.DataSource = list; //var map = mappingcontrol.CreatehibernatemappingXML(list, tablename, _confg.schema, _namespace, _confg.assemly); //CommonHelper.XmlSerializer.SaveToXml("D:\\" + classname + ".xml", map, typeof(hibernatemapping), "hibernate-mapping"); mappingentity mapent = CreateMap(); //string entity = mappingcontrol.CreateEntity(list, tablename, _confg.schema, _namespace, _confg.assemly, mapent); } catch (Exception ex) { } }
mappingentity CreateMap() { mappingentity map = null; object obj = XmlSerializer.LoadFromXml(AppPath + @"mapping.xml", typeof(mappingentity)); map = obj as mappingentity; return(map); }
public static string CreateEntity(List <DbColumn> list, string tablename, string schema, string _namespace, string assembly, mappingentity map, string path) { StringBuilder sb = new StringBuilder(File.ReadAllText(path)); foreach (var item in map.map) { #region MyRegion switch (item.operate) { case "REPLACE": //switch (item.source) //{ // case "": // break; // default: // break; //} sb.Replace("<#" + "NAMESPACE" + "#>", _namespace + "." + tablename); sb.Replace("<#" + "ENTITYNAME" + "#>", tablename + "Entity"); break; default: break; } #endregion } Regex reg = new Regex(@"(?<=<#FOREACH#>)[\s\S]*?(?=<#/FOREACH#>)"); MatchCollection mats = reg.Matches(sb.ToString()); StringBuilder t = sb.Replace(@"<#FOREACH#>" + mats[0].Value + @"<#/FOREACH#>", ""); StringBuilder colsb = new StringBuilder(); foreach (var item in map.FOREACH) { #region MyRegion switch (item.source) { case "COLUMN": foreach (var col in list) { if ("Created,Updated,Deleted,IsDeleted,ID".ToLower().Contains(col.ColumnName.ToLower())) { continue; } StringBuilder property = new StringBuilder(mats[0].Value); #region MyRegion foreach (var i in item.map) { switch (i.source) { case "DESCRIPTION": property.Replace("<#DESCRIPTION#>", string.IsNullOrEmpty(col.Remark) ? col.ColumnName : col.Remark); break; case "COLUMNNAME": property.Replace("<#COLUMNNAME#>", col.ColumnName); break; case "TYPE": property.Replace("<#TYPE#>", col.CSharpType); break; default: break; } } #endregion colsb.Append(property); } break; default: break; } #endregion } t.Insert(mats[0].Index - "<#FOREACH#>".Length, colsb.ToString()); return(sb.ToString()); }