liveBook · Manning

CsvReader

As explained in Building Web APIs with ASP.NET Core

CsvReader is a CsvHelper class that parses CSV records and maps them to .NET objects.

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

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Explains configuration, file access, record enumeration, and persistence, but stops short of CsvHelper parser internals.
Examples
Uses bgg_dataset.csv, semicolon delimiters, BggRecord mapping, skipped rows, and BoardGame entity creation.
What it assumes
Assumes familiarity with dependency injection, StreamReader, CSV configuration, and Entity Framework Core database operations.
What you can do after
Apply it via CsvHelper to enumerate typed CSV records and feed validated data into a .NET persistence workflow.
How it connects
Connects CsvReader with CsvHelper, CsvConfiguration, StreamReader, BggRecord, and Entity Framework Core.

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.