Entity Framework Core 2.1 – Seed Data – Data in the Migration

Data Seeding refers to the idea of inserting predefined data in our tables. Sometimes we use this with tables whose data does not vary much, or for tables that have configuration data. A typical example would be to have a table of countries, and use Data Seeding to fill that table with all the countries of the planet. Something new in EF Core 2.1, is the idea of being able to associate this predefined data with a specific migration. This means that in a migration we can indicate that we want to insert data in one or more tables. In addition to that, we can remove that same data in another migration.

Data Seeding is done in the OnModelCreating method of our Data Context. We are going to see an example in which we insert a record in a table.

Note: In this post I will work with Visual Studio 15.7 preview 5, .net core 2.1 preview 2, and EF Core 2.1 preview 2.

Suppose we have the following model:


public class Student
{
  public int Id { get; set; }
  public string Name { get; set; }
  public DateTime Birthdate { get; set; }
  public bool IsDeleted { get; set; }
}

Adding Data

To use Data Seeding, we can place the following code in the OnModelCreating method of our Data Context:


var student1 = new Student() { Id = 7, Name = "Robert Seed", Birthdate = new DateTime(1990, 4, 12), IsDeleted = false };

modelBuilder.Entity<Student>().HasData(student1);

If you notice, what we do is instantiate a new student, and then we use the HasData function to be able to tell the EF Core that we want to insert this record in a migration. The HasData method is also capable of receiving a list of objects in case you want to create several records. Notice that we must specify the Id of the record that we are going to insert. Otherwise, it throws an error.

If you do an Add-Migration in the Package Manager Console, or with the dotnet CLI, we will have the following migration created:


protected override void Up(MigrationBuilder migrationBuilder)

{
  migrationBuilder.InsertData(
  table: "Students",
  columns: new[] { "Id", "Birthdate", "IsDeleted", "Name" },
  values: new object[] { 7, new DateTime(1990, 4, 12, 0, 0, 0, 0,
  DateTimeKind.Unspecified), false, "Robert Seed" });
}

Basically what we have is an InsertData method that what it does is insert the data of the record that we indicated in the OnModelCreating. If we do an Update-Database, then this record will be inserted into the corresponding table.

This type of techniques are especially useful with databases in which we have tables that serve as configuration or that have relatively static data.

Deleting Data

In addition to adding data, we can delete data that we have inserted via the HasData method. All we have to do is remove the object that we inserted in the HasData method and that’s it. Let’s see this. Remove the code from the OnModelCreating method that we inserted recently. Now, do an Add-Migration. This will result in the following Up method in our migration:


protected override void Up(MigrationBuilder migrationBuilder)
{
  migrationBuilder.DeleteData(
  table: "Students",
  keyColumn: "Id",
  keyValue: 7);
}

Notice that what you do is make a DeleteData, to erase the record in the Students table with Id equal to 7.

Summary

A new functionality of Entity Framework Core 2.1 is to be able to have data in our migrations. For that we can use the HasData method in the ModelBuilder of our OnModelCreating method of the Data Context.

3 comments

Leave a reply to gavilanch Cancel reply