public void CreateTable <T>(T t) where T : class { try { string tableID = PrimaryKeyHelper.MakePrimaryKey(PrimaryKeyHelper.PrimaryKeyType.Table, PrimaryKeyHelper.PrimaryKeyLen.V1); string tableName = typeof(T).Name; string primaryKey = "SystemID"; string Account = "sys"; string NickName = "系统生成"; Log_Table entity = new Log_Table(); entity.TableID = tableID; entity.TableName = tableName; entity.PrimaryKey = primaryKey; entity.Account = Account; entity.NickName = NickName; List <Log_TableDetails> lists = new List <Log_TableDetails>(); PropertyInfo[] pi = typeof(T).GetProperties(); foreach (PropertyInfo p in pi) { string columnName = p.Name.ToString(); //得到属性的名称 Type columnType = p.PropertyType; //得到属性的类型 if (p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>)) { columnType = p.PropertyType.GetGenericArguments()[0]; } lists.Add(new Log_TableDetails() { TableID = tableID, TableName = tableName, ColumnName = columnName, ColumnDataType = columnType.Name, Account = Account, NickName = NickName, }); } TableService.SaveTable(entity); TableDetailsService.SaveTableDetails(lists); } catch (Exception ex) { throw new Exception(ex.Message); } }
private Log_Table SaveTable(string tableName) { try { string tableID = CreateTableID(); Log_Table entity = new Log_Table(); entity.TableID = tableID; entity.TableName = tableName; entity.PrimaryKey = PrimaryKey; entity.Account = Account; entity.NickName = NickName; List <Log_TableDetails> lists = new List <Log_TableDetails>(); PropertyInfo[] pi = typeof(T).GetProperties(); foreach (PropertyInfo p in pi) { string columnName = p.Name.ToString(); //得到属性的名称 Type columnType = p.PropertyType; //得到属性的类型 if (p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>)) { columnType = p.PropertyType.GetGenericArguments()[0]; } lists.Add(new Log_TableDetails() { TableID = tableID, TableName = tableName, ColumnName = columnName, ColumnDataType = columnType.Name, Account = Account, NickName = NickName, }); } TableService.SaveTable(entity); TableDetailsService.SaveTableDetails(lists); return(entity); } catch (Exception ex) { throw new Exception(ex.Message); } }