public MainFrm() { InitializeComponent(); InitFrm(); Instance = this; }
void CreateEntityClass() { if (tv_DBServers.SelectedNode != null && tv_DBServers.SelectedNode.Level == 3) { bool hasKey = false; var tbDesc = Biz.Common.Data.MySQLHelper.GetTableColsDescription(GetDBSource(tv_DBServers.SelectedNode), tv_DBServers.SelectedNode.Parent.Text, tv_DBServers.SelectedNode.Text); Regex rg = new Regex(@"(\w+)\s*\((\w+)\)"); string format = @" {4}public {0} {1} {{ get {{ return {2}; }} set {{ {3}=value; }} }}"; //命名空间 SubForm.CreateEntityNavDlg dlg = new CreateEntityNavDlg("请输入实体命名空间", DefaultEntityNamespace); if (dlg.ShowDialog() == DialogResult.OK) { DefaultEntityNamespace = dlg.InputString; } StringBuilder sb = new StringBuilder(string.Format("namespace {0}\r\n", DefaultEntityNamespace)); sb.AppendLine("{"); sb.AppendLine(" [Serializable]"); if (dlg.SupportProtobuf) { sb.AppendLine(" [ProtoContract]"); } if (dlg.SupportDBMapperAttr) { sb.AppendLine(" [DataBaseMapperAttr(TableName=\"" + tv_DBServers.SelectedNode.Text + "\")]"); } sb.Append(" public class "); sb.Append(Biz.Common.StringHelper.FirstToUpper(tv_DBServers.SelectedNode.Text)); sb.Append("Entity"); sb.Append("\r\n {\r\n"); if (dlg.SupportDBMapperAttr) { sb.AppendLine(" //表名"); sb.AppendLine(string.Format(" public const string TbName=\"{0}.{1}\";", tv_DBServers.SelectedNode.Parent.Text, tv_DBServers.SelectedNode.Text)); } sb.AppendLine(); sb.AppendLine(@" //分表 public string SplitTbName { get { throw new NotImplementedException(); } }"); sb.AppendLine(); TreeNode selNode = tv_DBServers.SelectedNode; int idx = 1; foreach (TreeNode node in selNode.Nodes) { if (node.Text == "索引" && node == selNode.LastNode) { continue; } Match m = rg.Match(node.Text); if (m.Success) { var y = (from x in tbDesc.AsEnumerable() where string.Equals((string)x["ColumnName"], m.Groups[1].Value, StringComparison.OrdinalIgnoreCase) select x["Description"]).FirstOrDefault(); string desc = y == DBNull.Value ? string.Empty : (string)y; string privateAttr = string.Concat("_" + Biz.Common.StringHelper.FirstToLower(m.Groups[1].Value)); sb.AppendFormat(" private {0} {1};", Biz.Common.Data.Common.DbTypeToNetType(m.Groups[2].Value), privateAttr); sb.AppendLine(); if (dlg.SupportProtobuf) { sb.AppendLine(string.Format(" [ProtoMember({0})]", idx++)); } bool iskey = false; if (node.Tag != null && node.Tag is TBColumn) { iskey = (node.Tag as TBColumn).IsID || (node.Tag as TBColumn).IsKey; } if (dlg.SupportDBMapperAttr) { if (iskey) { sb.AppendLine(" [DataBaseMapperAttr(Column=\"" + m.Groups[1].Value + "\",isKey=true)]"); hasKey = true; } else { sb.AppendLine(" [DataBaseMapperAttr(Column=\"" + m.Groups[1].Value + "\")]"); } } if (dlg.SupportJsonproterty) { sb.AppendLine(" [JsonProperty(\"" + m.Groups[1].Value.ToLower() + "\")]"); sb.AppendLine(" [PropertyDescriptionAttr(\"" + desc + "\")]"); } sb.AppendFormat(format, Biz.Common.Data.Common.DbTypeToNetType(m.Groups[2].Value), Biz.Common.StringHelper.FirstToUpper(m.Groups[1].Value), privateAttr, privateAttr, dlg.SupportMvcDisplay ? string.Format("[Display(Name = \"{0}\")]\r\n ", desc) : string.Empty); sb.AppendLine(); } else { MessageBox.Show("生成实体类错误:" + node.Text); break; } } sb.AppendLine(" }"); sb.AppendLine("}"); if (OnCreateEntity != null) { OnCreateEntity("实体类" + selNode.Text, sb.ToString()); } Clipboard.SetText(sb.ToString()); MainFrm.SendMsg(string.Format("实体代码已经复制到剪贴板,{0}", hasKey ? "" : "警告:表没有自增主键。")); } }