private static List <TEntity> MapQueryToEntities(NpgsqlDataReader reader) { var result = new List <TEntity>(); List <TableColumn> columns = MetadataResolver.TableColumns <TEntity>(); Dictionary <string, TableColumn> columnMap = columns.ToDictionary(i => i.Name, i => i); while (reader.Read()) { var entity = new TEntity(); for (int i = 0; i < reader.FieldCount; i++) { string columnName = reader.GetName(i); if (columnMap.TryGetValue(columnName, out TableColumn column)) { object columnValue = reader.GetValue(i); column.SetValue(entity, columnValue); } else { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine($"Warning: Failed to map db column '{columnName}' a TableColumn on entity '{typeof(TEntity)}' (did the schema change recently? is a db upgrade required?)"); Console.ResetColor(); } } result.Add(entity); } return(result); }
private List <string> GetColumnDefinitions() { List <string> definitions = MetadataResolver.TableColumns <TEntity>() .Select(c => c.DefinitionForCreateTable()) .ToList(); List <string> compositeKeys = MetadataResolver.CompositePrimaryKeys <TEntity>(); if (!compositeKeys.IsNullOrEmpty()) { definitions.Add( $"PRIMARY KEY({string.Join(", ", compositeKeys)})"); } return(definitions); }
public InsertCommand( Func <NpgsqlConnection> getConnection, List <TEntity> entities) { _getConnection = getConnection ?? throw new ArgumentNullException(nameof(getConnection)); if (entities.IsNullOrEmpty()) { throw new ArgumentNullException(nameof(entities), "At least one entity must be provided for an Insert command."); } List <TableColumn> columns = MetadataResolver.TableColumns <TEntity>(); AppendInsertInto(columns); AppendEntityValues(entities, columns); }