static void GetColumns(Table table) { var pis = table.Type.GetProperties(); var hasKey = false; foreach (var property in pis) { var columnAttr = (ColumnAttribute)property.GetCustomAttributes(_columnAttrType, false).FirstOrDefault(); var keyAttr = (KeyAttribute)property.GetCustomAttributes(_keyAttrType, true).FirstOrDefault(); var genAttr = (DatabaseGeneratedAttribute)property.GetCustomAttributes(_dataBaseGeneratedAttrType, true).FirstOrDefault(); Column column = new Column(); column.Table = table; column.PropertyInfo = property; column.Name = property.Name; column.IsKey = keyAttr != null; if (column.IsKey) { hasKey = true; } if (column.IsKey) { column.IsAutoIncreament = true; } if (genAttr != null) { column.IsAutoIncreament = genAttr.DatabaseGeneratedOption == DatabaseGeneratedOption.Identity; column.IsKey = true; hasKey = true; } if (columnAttr != null) { column.Name = columnAttr.Name; } table.Columns.Add(column.PropertyInfo.Name, column); } if (!hasKey) { var column = table.Columns.Get("Id"); if (column != null) { column.IsKey = true; column.IsAutoIncreament = true; } } }
/// <summary> /// 根据给定的类型分析表名、数据库名 /// </summary> /// <param name="entityType"></param> /// <returns></returns> public static Table GetTable(Type entityType) { var table = _tableTypeMap.Get(entityType); if (table != null) { return table; } lock (_tableTypeMap) { table = _tableTypeMap.Get(entityType); if (table != null) { return table; } if (!IsEntity(entityType)) { throw new Exception("不是实体类型"); } var tableAttr = (TableAttribute)entityType.GetCustomAttributes(_tableAttrType, true).FirstOrDefault(); string tableName, dbName = null; table = new Table(); if (tableAttr != null) { tableName = tableAttr.Name; } else { tableName = StringHelper.ToPlural(entityType.Name); } var dbTableAttr = (DataBaseAttribute)entityType.GetCustomAttributes(_dataBaseAttrType, true).FirstOrDefault(); if (dbTableAttr != null) { dbName = dbTableAttr.Name; } table.DataBase = dbName; table.Name = tableName; table.Type = entityType; GetColumns(table); _tableTypeMap.Add(entityType, table); return table; } }
public abstract string GetTableName(SchemaModel.Table table);