Data Seeding and Custom Migrations

Learn how to seed data and use raw SQL in migrations.

Data seeding

Data seeding involves populating the database with initial data. It is useful for the following:

  • Providing test data in a development environment.
  • Setting up initial data in a development/production environment.

Note: The commands in this lesson generate code and files. Through the terminal, we can navigate to these files by using relevant Linux commands such as ls to view a list of files and directories, cd to change directories, and cat to view file contents. A SPA widget showing the updated project with the generated files is also available. Also, note that EF Core uses a timestamp within the generated file names. We represent these names with xxx.

In an earlier lesson, we mentioned that the Tags table holds a set of categories for the artists’ albums. In this section, we use migrations to provide initial data for this table. One way to achieve this is via the OnModelCreating method of the context class. We’ll demonstrate using the project below:

Get hands-on with 1200+ tech skills courses.