Seed database with test data using Entity Framework

seed-database
Step 1: Right click on the project in solution explorer and add a class file with name = EmployeeDBContextSeeder.cs

Step 2: Copy and paste the following code in EmployeeDBContextSeeder.cs file

using System.Collections.Generic;
using System.Data.Entity;
namespace EntityFrameworkDemo
{
 public class EmployeeDBContextSeeder :
 DropCreateDatabaseIfModelChanges
 {
 protected override void Seed(EmployeeDBContext context)
 {
 Department department1 = new Department()
 {
 Name = "IT",
 Location = "New York",
 Employees = new List()
 {
 new Employee()
 {
 FirstName = "Mark",
 LastName = "Hastings",
 Gender = "Male",
 Salary = 60000,
 JobTitle = "Developer"
 },
 new Employee()
 {
 FirstName = "Ben",
 LastName = "Hoskins",
 Gender = "Male",
 Salary = 70000,
 JobTitle = "Sr. Developer"
 },
 new Employee()
 {
 FirstName = "John",
 LastName = "Stanmore",
 Gender = "Male",
 Salary = 80000,
 JobTitle = "Project Manager"
 }
 }
 };

Department department2 = new Department()
 {
 Name = "HR",
 Location = "London",
 Employees = new List()
 {
 new Employee()
 {
 FirstName = "Philip",
 LastName = "Hastings",
 Gender = "Male",
 Salary = 45000,
 JobTitle = "Recruiter"
 },
 new Employee()
 {
 FirstName = "Mary",
 LastName = "Lambeth",
 Gender = "Female",
 Salary = 30000,
 JobTitle = "Sr. Recruiter"
 }
 }
 };
 Department department3 = new Department()
 {
 Name = "Payroll",
 Location = "Sydney",
 Employees = new List()
 {
 new Employee()
 {
 FirstName = "Steve",
 LastName = "Pound",
 Gender = "Male",
 Salary = 45000,
 JobTitle = "Sr. Payroll Admin",
 },
 new Employee()
 {
 FirstName = "Valarie",
 LastName = "Vikings",
 Gender = "Female",
 Salary = 35000,
 JobTitle = "Payroll Admin",
 }
 }
 };

context.Departments.Add(department1);
 context.Departments.Add(department2);
 context.Departments.Add(department3);

base.Seed(context);
 }
 }
}

Step 3: Copy and paste the following line in Application_Start() method Global.asax file


Database.SetInitializer(new EmployeeDBContextSeeder());

Step 4: Remove the following Table and Column attributes from Employee.cs file.
[Table(“tblEmployees”)]
[Column(“First_Name”)]
At this point Employee class should look as shown below

public class Employee
{
 public int Id { get; set; }
 public string FirstName { get; set; }
 public string LastName { get; set; }
 public string Gender { get; set; }
 public int Salary { get; set; }
 public int DepartmentId { get; set; }
 [ForeignKey("DepartmentId")]
 public Department Department { get; set; }
 public string JobTitle { get; set; }
}

Step 5: Run the application and notice that the Sample database, Departments and Employees tables are created and populated with test data automatically.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s