CsvReader is the CsvHelper component that reads CSV content and maps each record to a .NET object such as BggRecord. In the board-game API, it turns bgg_dataset.csv rows into data that can seed the database. section 5.3.4 section 5.3.5
Prepare CsvReader in a seeding action by:
- Creating CsvConfiguration with the appropriate culture, header setting, and field delimiter. section 5.3.5
- Opening bgg_dataset.csv with StreamReader using the application content-root path. section 5.3.4 section 5.3.5
- Constructing CsvReader with the reader and configuration, then retrieving records with GetRecords<BggRecord>(). section 5.3.5
- Iterating through records, skipping invalid or existing rows, creating entities, and saving them with Entity Framework Core. section 5.3.5
Reading the board-game dataset
A configured CsvReader reads semicolon-delimited records using pt-BR culture. GetRecords<BggRecord>() supplies the rows; the seeding loop converts valid records into BoardGame entities and counts skipped rows. section 5.3.5
var config = new CsvConfiguration(CultureInfo.GetCultureInfo("pt-BR"))
{
HasHeaderRecord = true,
Delimiter = ";",
};
using var reader = new StreamReader(
System.IO.Path.Combine(_env.ContentRootPath, "Data/bgg_dataset.csv"));
using var csv = new CsvReader(reader, config);
var existingBoardGames = await _context.BoardGames
.ToDictionaryAsync(bg => bg.Id);
var existingDomains = await _context.Domains
.ToDictionaryAsync(d => d.Name);
var existingMechanics = await _context.Mechanics
.ToDictionaryAsync(m => m.Name);
var now = DateTime.Now;
Inspect the complete setup showing CsvConfiguration, StreamReader, the content-root file path, and CsvReader construction. section 5.3.5
