Example #1
0
 /// <summary>
 /// 设置类名和表名的映射  key 为类名  value 为表名
 /// </summary>
 /// <param name="mappingTables"></param>
 public void SetMappingTables(List<KeyValuePair> mappingTables)
 {
     if (mappingTables.IsValuable())
     {
         _mappingTableList = mappingTables;
     }
 }
Example #2
0
        /// <summary>
        /// 根据DataTable获取实体类的字符串
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="className"></param>
        /// <param name="nameSpace"></param>
        /// <param name="dataTableMapList"></param>
        /// <returns></returns>
        public string DataTableToClass(DataTable dt, string className, string nameSpace = null, List<PubModel.DataTableMap> dataTableMapList = null)
        {
            StringBuilder reval = new StringBuilder();
            StringBuilder propertiesValue = new StringBuilder();
            string replaceGuid = Guid.NewGuid().ToString();

            var template = ClassTemplate.Template;
            string _ns = nameSpace;
            string _foreach = "";
            string _className = className;
            List<string> _primaryKeyName = new List<string>();
            foreach (DataColumn r in dt.Columns)
            {
                propertiesValue.AppendLine();
                string typeName = ChangeType(r.DataType);
                bool isAny = false;
                PubModel.DataTableMap columnInfo = new PubModel.DataTableMap();
                if (dataTableMapList.IsValuable())
                {
                    isAny = dataTableMapList.Any(it => it.COLUMN_NAME.ToString() == r.ColumnName);
                    if (isAny)
                    {
                        columnInfo = dataTableMapList.First(it => it.COLUMN_NAME.ToString() == r.ColumnName);
                        if (columnInfo.IS_PRIMARYKEY.ToString() == "1")
                        {
                            _primaryKeyName.Add(r.ColumnName);
                        }
                        propertiesValue.AppendFormat(ClassTemplate.ClassFieldSummaryTemplate,
                        columnInfo.COLUMN_DESCRIPTION.IsValuable() ? columnInfo.COLUMN_DESCRIPTION.ToString() : "-", //{0}
                        columnInfo.COLUMN_DEFAULT.IsValuable() ? columnInfo.COLUMN_DEFAULT.ToString() : "-", //{1}
                        Convert.ToBoolean(columnInfo.IS_NULLABLE));//{2}
                    }
                }
                propertiesValue.AppendFormat(
                    ClassTemplate.ItemTemplate,
                    isAny ? ChangeNullable(typeName, Convert.ToBoolean(columnInfo.IS_NULLABLE)) : typeName,
                    r.ColumnName, "{get;set;}",
                    "");
                propertiesValue.AppendLine();
            }
            _foreach = propertiesValue.ToString();

            template = ClassTemplate.Replace(template, _ns, _foreach, _className, _primaryKeyName);
            return template;
        }