liveBook · Manning

Data seeding

As explained in Building Web APIs with ASP.NET Core

Database seeding is the process of populating a database with an initial, required, or imported set of records.

Data seeding populates a database with an initial, required, or imported set of records. In a board-game API, it replaces temporary dummy data with records loaded into real database tables, allowing normal retrieval and later CRUD operations. section 5.2.2 section 5.3

A practical CSV seeding workflow consists of these steps:

  • Place the CSV file in a project folder and install a package that maps CSV rows to C# objects. section 5.3
  • Define a BggRecord POCO, then create a SeedController action dedicated to importing the data. section 5.3
  • Configure CsvHelper, open the file with StreamReader, parse records, and map each record to EF Core entities. section 5.3.5
  • Skip rows without required identifiers or names and skip records already present in the database. section 5.3.5
  • Save the new entities and return counts for imported tables and skipped rows. section 5.3.5

An idempotent seed endpoint

Expose the import action through HTTP PUT, disable response caching, and check existing records before insertion. Repeating the request therefore leaves already imported data unchanged. Keep unrelated initialization tasks separate: board-game data can use /Seed/BoardGameData, while roles and users use /Seed/AuthData. section 5.3.5 section 9.4.2

Figure 5.1 BoardGameController’s Get() method returning an empty “data” array
05-01

Use the empty data-array response to verify why seeding is necessary after switching retrieval from dummy data to a database query. section 5.2.2

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Details file placement, mapping, parsing, validation, duplicate checks, persistence, and endpoint design, but not deployment automation.
Examples
Uses a board-game CSV import, SeedController endpoints, duplicate checks, skipped-row counts, and role initialization.
What it assumes
Assumes familiarity with C#, ASP.NET Core controllers, databases, and EF Core while teaching CSV mapping and idempotent requests.
What you can do after
Build it from scratch by implementing an idempotent CSV import endpoint that maps records into EF Core entities.
How it connects
Connects CSV parsing, POCO mapping, EF Core, HTTP idempotency, controllers, and CRUD operations.

Also explained in

Compare how 2 books explain data seeding →

Related concepts

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.