コード例 #1
0
        /// <summary>
        /// 生成实体类
        /// </summary>
        /// <param name="dbTableName">数据库中的表名称</param>
        /// <param name="tableCamelName">参与生成的表名称(可能是数据表名称也可能是表名称的骆驼表示法</param>
        public void GenerateModel(Enumeration.DataBaseType DBType, ModelTable mt)
        {
            StringBuilder sbTemp    = new StringBuilder();
            DataTable     dtColumns = SelectColumnsByTableName(DBType, mt.Table_Name);

            string colName = string.Empty;

            #region 生成命名空间和类
            sbTemp.Append("using System;"); //引入命名空间
            sbTemp.Append("\r\nusing Nest;");
            sbTemp.Append("\r\nusing System.ComponentModel;");
            sbTemp.Append("\r\nusing System.Collections.Generic;");
            sbTemp.Append("\r\nusing System.ComponentModel.DataAnnotations;");
            sbTemp.Append("\r\nusing System.ComponentModel.DataAnnotations.Schema;");
            sbTemp.Append("\r\n");
            sbTemp.Append("\r\n").Append("namespace ").Append(NameSpaceModel);    //命名空间
            sbTemp.Append("\r\n").Append("{");
            sbTemp.Append("\r\n").Append("\t/// <summary>");
            sbTemp.Append("\r\n").Append("\t///").Append(GetTableComments(DBType, mt.Table_Name)
                                                         .Replace("\r\n", " ")
                                                         .Replace("\n", " "));
            sbTemp.Append("\r\n").Append("\t/// </summary>");
            sbTemp.Append("\r\n").Append("\t[Serializable]");
            sbTemp.Append("\r\n").Append("\t[Table(\"" + mt.Table_Name + "\")]");
            sbTemp.Append("\r\n").Append("\t[ElasticsearchType(Name = \"" + mt.Table_Name + "\")]");
            sbTemp.Append("\r\n").Append("\tpublic partial class ")
            .Append(Words.ToSingular(Words.reWriteWord(mt.TabCamelName)));
            sbTemp.Append("\r\n").Append("\t{");    //类
            #endregion

            #region 生成属性
            for (int i = 0; i < dtColumns.Rows.Count; i++)
            {
                //string tempTableName = dtColumns.Rows[i]["TABLE_NAME"].ToString();
                string tempColumnName = dtColumns.Rows[i]["COLUMN_NAME"].ToString();
                tempColumnName = tempColumnName == mt.Table_Name ? tempColumnName + "1" : tempColumnName;//如果字段名和表明相同,就在字段名后面加上字符"1"
                colName        = tempColumnName;

                string tempType        = dtColumns.Rows[i]["DATA_TYPE"].ToString();
                string tempLength      = dtColumns.Rows[i]["DATA_LENGTH"].ToString();
                string tempPrecision   = dtColumns.Rows[i]["DATA_PRECISION"].ToString();
                string tempScale       = dtColumns.Rows[i]["DATA_SCALE"].ToString();
                string tempNullAble    = dtColumns.Rows[i]["NULLABLE"].ToString();
                string tempDescription = dtColumns.Rows[i]["COMMENTS"].ToString().Replace("\r\n", " ").Replace("\n", " ");

                //if (string.IsNullOrWhiteSpace(tempScale))
                //{
                //    tempScale = "0";
                //}

                //属性
                sbTemp.Append("\r\n");
                sbTemp.Append("\r\n\t\t/// <summary>");
                sbTemp.Append("\r\n\t\t///").Append(tempDescription);
                sbTemp.Append("\r\n\t\t/// </summary>");
                //[Nest.String(Index = FieldIndexOption.NotAnalyzed)]
                //[Nest.String(Analyzer = "standard")]

                sbTemp.Append("\r\n\t\t[Display(Name = \"").Append(PublicHelper.GetCamelName(colName)).Append("\")]");
                if (tempNullAble == "N" || tempNullAble == "NO")
                {
                    sbTemp.Append("\r\n\t\t[Required]");
                }
                string ColType = DataTypeConvert.ConvertTypeVS2008(dtColumns.Rows[i]);
                if (ColType == "string")
                {
                    if (tempLength == "")
                    {
                        if (tempType == "text" || tempType == "character varying")
                        {
                            sbTemp.Append("\r\n\t\t[StringLength(").Append("8000").Append(")]");
                        }
                        else
                        {
                            MessageBox.Show("提示!", "未知类型:" + tempType, MessageBoxButtons.OK,
                                            MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
                        }
                    }
                    else
                    {
                        sbTemp.Append("\r\n\t\t[StringLength(").Append(tempLength).Append(")]");
                    }
                }
                //sbTemp.Append("\r\n\t\t[Column(TypeName = \"").Append(ColType).Append("\")]");

                //是否主键
                if (mt.PrimayKey.Equals(colName))
                {
                    sbTemp.Append("\r\n\t\t[Key]");
                    sbTemp.Append("\r\n\t\t[Keyword]");
                }

                sbTemp.Append("\r\n\t\tpublic ")
                .Append(ColType)
                .Append(" ")
                .Append(Words.reWriteWord(colName))
                .Append(" { get; set; }");
            }
            sbTemp.Append("\r\n\t}");
            sbTemp.Append("\r\n}");
            #endregion

            // 生成cs文件
            generateFile("Models", Words.ToSingular(mt.TabCamelName), sbTemp);
        }