Entity Framework's Database Seed Method

The Entity Framework can automatically create/update/drop databases when the application runs. We can specify that, this should be done every time application runs or only when the model is out of sync with the existing database or in other word it runs whenever model changes. You spent time to insert records in database and when you made any changes in model, Entity Framework deletes your database as well as records.

Entity Framework recommends to use 'Database Migrations' to stop loosing Database or Records. Usually we don't use 'Database Migrations' in our normal application development and for this Entity Framework got Database 'Seed' Method that allow you to seed some dummy data in the database for testing purpose and this could be used in MVC, ASP.NET Web Forms, Windows Form apps etc.

Creating a database initializer can be done by inheriting from either of those generic classes:
  • DropCreateDatabaseIfModelChanges<DBContext>
  • DropCreateDatabaseAlways<DBContext>
Now, follow the steps to setup a demo Console Application and then will explore ‘Seed Method’.

Step 1: Create New Project

Create a new console application File > New > Project > Visual C# > Console Application.

Step 2: Install EF5 from NuGet

At very first, we need to enable the Entity Framework 5 for this console project from NuGet Package Manager. For this, in ‘Solution Explorer’ right click on project and click on ‘Manage NuGet Packages’ and install Entity Framework 5. Alternatively, you can install this using power console by using command ‘Install-Package EntityFramework’.


Step 3: Create Model

Now, time to define a new model using Code First Approach.

    public class Student
    {
        public int StudentId { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
    }

Step 4: Create DbContext

Now, go ahead and define the DbContext.

    public class StudentContext : DbContext
    {
        public DbSet<Student> Students { get; set; }
    }

Note to use a namespace ‘using System.Data.Entity;’ on the top.

Step 5: Database Initializer (Dummy Data)

Now, we done with Model and DbContext, let’s decide what data we want to initialize when database first created. This can be done using any class that inherits DropCreateDatabaseIfModelChanges<StudentContext> or DropCreateDatabaseAlways<StudentContext>. First one will be called when model get change and second one will be called each time we run the application, I’m using the first one. We can place this code in a separate class file also.

    public class StudentDbInitializer : DropCreateDatabaseIfModelChanges<StudentContext>
    {
        protected override void Seed(StudentContext context)
        {
            var students = new List<Student>
            {
                new Student { Name = "Abhimanyu K Vatsa", Address = "Bokaro" },
                new Student { Name = "Deepak Kumar Gupta", Address = "Bokaro" },
                new Student { Name = "Manish Kumar", Address = "Muzaffarpur" },
                new Student { Name = "Rohit Ranjan", Address = "Patna" },
                new Student { Name = "Shivam", Address = "Motihari" }
            };
            students.ForEach(s => context.Students.Add(s));
            context.SaveChanges();
        }
    }

Step 6: Seed Dummy Data to Database

So, we have dummy data that I will seed to database and database will be created at run time. Now, we need make a call, here you go.

Database.SetInitializer<StudentContext>(new StudentDbInitializer());

Step 6: Complete Code

Find the complete code of my console application.

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication5_Seed
{
    public class Student
    {
        public int StudentId { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
    }

    public class StudentContext : DbContext
    {
        public DbSet<Student> Students { get; set; }
    }

    public class StudentDbInitializer : DropCreateDatabaseIfModelChanges<StudentContext>
    {
        protected override void Seed(StudentContext context)
        {
            var students = new List<Student>
            {
                new Student { Name = "Abhimanyu K Vatsa", Address = "Bokaro" },
                new Student { Name = "Deepak Kumar Gupta", Address = "Bokaro" },
                new Student { Name = "Manish Kumar", Address = "Muzaffarpur" },
                new Student { Name = "Rohit Ranjan", Address = "Patna" },
                new Student { Name = "Shivam", Address = "Motihari" }
            };
            students.ForEach(s => context.Students.Add(s));
            context.SaveChanges();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            using (var context = new StudentContext())
            {
                Database.SetInitializer<StudentContext>(new StudentDbInitializer());

                var stdQuery = (from d in context.Students
                                select new { Id = d.StudentId, Name = d.Name });

                foreach (var q in stdQuery)
                {
                    Console.WriteLine("ID : " + q.Id + ", Name : " + q.Name);
                }

                Console.ReadKey();
            }
        }
    }
}

Now, if you run the application you will see all the dummy data that we have created and initialized above.



Step 7: MVC Apps

I am an ASP.NET guy, so I'd like to tell something here on MVC. In MVC we call the SetInitializer method from Application_Start() method that can be found in Global.asax.cs file. Find here a sample:

        protected void Application_Start()
        {
            System.Data.Entity.Database.SetInitializer(new StudentDbInitializer());

            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }


I hope you like it. Thanks.

Comments

  1. Error.....
    Model compatibility cannot be checked because the database does not contain model metadata. Model compatibility can only be checked for databases created using Code First or Code First Migrations.

    ReplyDelete
  2. thanks. worked for me

    ReplyDelete
  3. Does this works for Model-First too?

    ReplyDelete
  4. This article was really helpful today, thanks man. I'm working with EF 6.2 and code first approach.

    ReplyDelete

Post a Comment

Popular posts from this blog

Migrating database from ASP.NET Identity to ASP.NET Core Identity

Customize User's Profile in ASP.NET Identity System