One of the new EF Core 2.1 features is seeding of data. Contrary to the older version of Entity Framework, seeding with EF Core works together with migrations. This article shows how data seeding can be used with EF Core 2.1.
Seeding Data in OnModelCreated
With the first sample, I’m defining a simple
Book type. Predefined books should be written to the database as the database is created. This can easily be achieved by using the
HasData method. This method is defined with the
EntityTypeBuilder that is returned from the
Entity method of the
HasData method is defined with the
params keyword to allow any number of entity types passed. With the sample code, three Book objects are created and passed to the
The values for the primary key values are required when seeding the data. This differs from the previous Entity Framework implementation, and is used to uniquely identify the records on migration scenarios.
The database can now created using the
EnsureCreated method of the
DatabaseFacade that is returned from the
Database property of the
Shadow state with Seeds
One of the great features of EF Core is shadow state. With shadow state, state defined in the table of the database is not mapped with the model type. This state can be retrieved and written using the context.
Defining state just with the context, also requires to pass this data when seeding the records. An overload of the
HasData method allows passing an object array. Here, you can pass objects of anonymous types and include all the shadow state with properties.
MenuItem class defines the properties
In addition to that, the
LastUpdated column is used in the
MenuItems table. This is defined as shadow state. With the
HasData method, anonymous objects are passed. This way it is possible to define any properties – including the shadow state value for
With the MenuItem items, the database is created in the same way as the previously created
Migrations and Seeding Data
What about migrations and data seeding? This time, the
Racer type with the
F1Context is created. Racers are seeded with the
First migrations are added to the project. This is done using the dotnet CLI:
dotnet ef migrations add InitF1 --context F1Context
Adding migrations, adds migration and snapshot classes. The first migration class,
InitF1, defines to create the table
Racers including racer values taken from the
The database is created applying the migrations programmatically.
Next, another property is added to the
Racer type, the
Point property. With the model creation functionality, the points for the racers now need to be included as well:
What about adding a migration now? With the migration, not only the
Points column need to be added to the databases, but also the existing records from the database need to be updated to include the new values. Let’s apply the new migration:
dotnet ef migrations add AddPoints --context F1Context
Applying this migration creates the
AddPoints migration class where the
Points column is created, and the existing records based on their id updated with the new values for points:
Data seeding is one of the great enhancements for EF Core 2.1. Seeding was already available with the .NET Framework version of the Entity Framework. Compared to the older implementation, the new version needed to be enhanced as Entity Framework didn’t support shadow state. This functionality is now available with data seeding for EF Core. The new implementation for EF Core also supports migrations. With Entity Framework, I didn’t use seeding data with many projects because migrations was not possible. This is now a feature with EF Core, and data seeding shines new.
If you read that far, it would be great if you support my writing by buying me a coffee. This helps staying up longer and writing more articles. You can also add information about what topics you’re in special interested in – I will consider this.
Enjoy learning and programming!