Seed Data in Entity Framework Core – Session 49

Seed Data in Entity Framework Core

In this session, we discuss Seed Data in Entity Framework Core and sync Domain Models with the database. Also, we will show you how you can remove a migration that was already synced with the database. During this session, we implement seeding data with two methods. First, we seed data inside the DB context, and then we use the ModelBuilder extension.

Seed Data inside DB Context in EF Core

For this purpose, we need to override OnModelCreating inside DB Context.

Then, with the object of ModelBuilder and HasData method, we can seed the data.

For syncing the changes and seeding the data to the Database, we need to run Add-Migration and Update-Database commands that we already discussed.

Seed Data with ModelBuilder Extension

To keep DB Context as clear as possible, we can move Seeding Data inside an extension of ModelBuilder. Thus, we need to create a Public Static class which should have a Public Static Void method that has an object of ModelBuilder as an input parameter.

Then, as shown above, with the object of ModelBuilder and HasData method, we can seed the data.

Also, we need to call this extension inside the DB Context class.

Also, as mentioned we need to run Add-Migration and Update-Database commands to sync the changes with the Database.

Sync Domain Models with Database in EF Core

During the development of a project, we need to modify the Domain Models based on our requirements. Hence, we need to sync these changes with the Database to have a unified structure on both sides. In EF Core, as we discussed in detail earlier, we should apply the changes to the database with Add-Migration and UpdateDatabase command.

When we run the Add-Migration command, EF Core creates a Class that contains Up and Down methods. Also, EF Core updated the Snapshot class based on the latest Model changes. Next, in the case of launching Update-Database, Up method of the created class is embarked and the changes will be applied to the Database. But, if we run the Remove-Migration command, the Snapshot class changes will be reverted and the auto-generated migration file will be deleted.

For better understanding, you can see a sample of the auto-generated Migration class.

Moreover, you can see a sample of the Snapshot class below.

Remove Synced Migration in EF Core

Normally, we can not remove a Migration that is already applied to the Database. Thus, we need to revert the applied changes from the Database. We can see the history of Migrations inside the EFMigrationsHistory table. Hence, we can find the name of the stage that we want to be reverted from that Table. Next, we can run the Update-Database command with the Migration Name to revert the changes that were applied after that migration. Thereupon, we can remove the Migrations with the Remove-Migration command.

Remove-Migration command removes the latest added migration each time.

If you need more details, watch this session video. Also, for being updated about our coming sessions, follow us on Instagram, Facebook, Telegram, or YouTube. Moreover, you can have access to the list of all sessions HERE and you can download this session source code from our GitHub.

You can download this Session Slides form HERE.

0.00 avg. rating (0% score) - 0 votes


4 1 vote
Article Rating

Inline Feedbacks
View all comments