Seeding Database with code-first workflow

by Sachin Singh


Posted on Wednesday, 20 January 2021

Tags: Seed database in code-first approach

When we enable migrations , visual studio automatically creates a configuration class for us which could be found under migration folder. We use this class to supply configuration to our migration. The default code is shown below.


   internal sealed class Configuration : DbMigrationsConfiguration
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(CodeFirstDemo.Program.OrganizationDbContext context)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data.
        }
    }

As you can see the configuration class contains a Seed() method , we use this method to initialize the database with some dummy data , as shown in below example.

Example :-Seed Employee and Department table with dummy data


  internal sealed class Configuration : DbMigrationsConfiguration
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(CodeFirstDemo.Program.OrganizationDbContext context)
        {
            context.Departments.AddOrUpdate(
                x => x.Name,  // decide add or update based on Name column
                new CodeFirstDemo.Program.Department() { Name = "Development", },
                 new CodeFirstDemo.Program.Department() { Name = "HR",  },
                  new CodeFirstDemo.Program.Department() { Name = "UI", },
                       new CodeFirstDemo.Program.Department() { Name = "BA", }
                );
            context.Employees.AddOrUpdate(
                x => x.Name,  // decide add or update based on Name column

                new CodeFirstDemo.Program.Employee() {Name="Sachin",DepId=2 },
                 new CodeFirstDemo.Program.Employee() { Name = "Arjun", DepId = 1 },
                  new CodeFirstDemo.Program.Employee() {Name="Vikash",DepId=2 },
                       new CodeFirstDemo.Program.Employee() {Name="Michael",DepId=2 }
                );         
        }
    }

Every time we execute Update-Database command from Package Manager Console, after executing all pending migrations this Seed() method is executed. Remember you should use the Seed() method against your development database only , you should not use this method to store reference data to your database. The reference data means the data which you want to deploy to production server for example data of Country table or Cities Table or Department Table etc. These data never change or rarely change. The correct way to seed reference data is to create and empty migration and then use SQL() helper extension method.

Step 1. To create an empty migration , add a migration without changing your model.


   PM> Add-Migration EmptyForSeedingDatabase

Step 2. Use SQL() method to seed data.


   public partial class EmptyForSeedingDatabase : DbMigration
    {
        public override void Up()
        {
            Sql("INSERT INTO [dbo].[Departments] VALUES ('Development')");
            Sql("INSERT INTO [dbo].[Departments]  VALUES ('HR')");
            Sql("INSERT INTO [dbo].[Departments]  VALUES ('UI')");
            Sql("INSERT INTO [dbo].[Departments]  VALUES ('BA')");
        }

        public override void Down()
        {
        }
    }


Seed data
How to seed data