This post is about how to seed database in ASP.NET Core. You may want to seed the database with initial users for various reasons. You may want default users and roles added as part of the application. In this post, we will take a look at how to seed the database with default data.
First you need to create a static class, SeedData with Initialize method. The class name and method name can be anything. You need to write the code to check whether Database is created or not. If it is created, write the EF Code to insert the data. Here is some pseudo code for the same.
Next you need to invoke the Initialize method. In ASP.NET Core 1.0, the recommended approach is calling it from Configure method in Startup class.
And in ASP.NET Core 2.0, the recommended approach is invoking it from main method in program class.
Happy Programming :)
What do you think? I would like to hear your thoughts, suggestions, and questions in the comments section below.