Use the migration function of ef core and configure seed data

Supporting code-first in Entity Framework Core, Entity Framework Core provides us with a migration function: to keep our database architecture design in sync with the application's model classes (also called entity classes). Only need to pay attention to the realization of business logic, the database will help us build it automatically.

image-20210609000019905

Open the console

image-20210609011553051
get-help about_entityframeworkcore
image-20210609000634236
Add-Migration
image-20210609000717684

This is the code automatically generated by efcore for us

image-20210609001918526
Update-Database
image-20210609000821517

Check out the database

image-20210609003749917
image-20210609003946979

But we found that this is an empty table, because we did not give the initial data

image-20210609010737051

Override methods in DbContext

image-20210609010902580

Add the required data and then

Add-Migration SeedStudentsTable(可变)Update-Database

Check again

image-20210609011152864

For the readability and maintainability of the code, the initialization seed can be extracted to write an extension method

image-20210609013410842
image-20210609013447335