Seeding the database with test data

Now that we have an empty database, let's fill it with some test data. To do this, we'll need to create a method that we will call after our database creation:

  1. Create a folder in your project called Data. In the folder, add a class called DbInitializer.cs:

The class has an Initialize method that takes our ResearchContext as a parameter:

public static void Initialize(ResearchContext context) 
  1. In the Initialize method, we first call the Database.EnsureCreated method to make sure the database exists and creates it if not:
context.Database.EnsureCreated(); 
  1. Next up, we do a quick Linq query to check if ...

Get C# 7 and .NET Core 2.0 Blueprints now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.