Seed Data in EF Core

EF Core Seed data means pre-populating the database with default data. This is useful in scenarios where you want to provide some test data in the development environment. You could use this to set up the application for the first time in a production environment by providing the sample or useful master data.

Source Code:
The source code of this project available in GitHub.

How to Seed Data in EF Core

In the EF Core, the database seeding has now become part of the Model Configuration. We discussed how to Configure the model using the Fluent API. This is done by overriding the OnModelCreating method.

OnModelCreating

The OnModelCreating method is called by the Context when it initializes the Model for the first time. The Context passes the reference to the ModelBuilder Instance in the OnModelCreating method.

We override the OnModelCreating method, where we get the reference to the Modelbuilder instance. We can use the HasData method of the ModelBuilder to seed the initial data into the database via migrations.

HasData 

The HasData method was introduced in EF CoOre 2.1. It is part of the EntityTypeBuilder<T> property of the ModelBuilder API, which we discussed in the Tutorial EF Core Fluent API

Example of Seeding Data

The following code shows the Example of Seeding Data in EF Core. Consider the Department Model as shown below

To Seed data in the Department table, First we need to override the OnModelCreating method

Next, we get the reference to the EntityTypeBuilder using the code;

Finally, use the HasData method to supply the list of Departments as shown above

Once you have added the initial data, you should use migrations to update the database 

The ID/Key values must be supplied in HasData method. The EF Core will not generate these when using the HasData method.  While inserting the data, the SQL Server will enable the insertion of Identity values. Also do not change the value of the Primary key.

Migrations

Unlike its predecessor entity framework, the EF Core data seeding works with the migrations. Hence we need to create the ef core migration to update the database.

and then update the database using update-database or use the script-migration to generate the SQL Script 

Updating the Seeded data

You can update the seeded data or add new data later by simply modifying the HasData method. For Example the following code changes the Department name from Production to Development

Run the add-migration v2 and update-database to update the database with the new department name.

Do not change the value of Primary key i.e DepartmentID. Because EF core uses it to check if any changes made to the record. If the changed field is a Foreign key, then the migration script will delete and recreate all the related records.

Never use some key generator functions for any field. For Example Guid.NewGuid() for a Guid field. DateTime.Now for a DateTime field. This will result in a new migration as the value changes every time you run the add-migration command. Hence always use hardcoded values.

Seeding Related Data

The related data is also added using the HasData method,

Example of Seeding Related Data

The following is the complete example of how to seed the database when various types of relationships are present.

Moving Seed Data to Separate File

The seeding method can get bigger and it would be a lot cleaner if we able to move the method to a separate class file. This is done by creating the extension method and calling the method from the OnModelCreating.

Create a class named ModelBuilderExtensions.cs and add the static seed method.

You can then call the Seed method from the OnModelCreating as shown below

References

1 thought on “Seed Data in EF Core”

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top