Seed the Database

Learn how to seed data into your application's database tables.

Introduction

“Seeding the database” means to store data into the database via code instead of having to manually add data after running the application. Seeding data is especially helpful for testing purposes. After testing is complete, the seeded data may possibly be replaced or removed.

How to seed a database in ASP.NET

Learn the new class you will need to create and which existing class you will need to update to seed your database.

Creating SeedData.cs

In the Models folder, you are creating a new class called SeedData. The purpose of this class is to check if any data already exists in your database tables. If nothing exists, then it will insert some provided objects as data. However, if some data is already present in your database, then nothing is inserted into your tables. Your class looks like this:

Get hands-on with 1400+ tech skills courses.