-1

I am using code first Approach in entity framework, but I am unable to seed the default data into the table. Please help.

Models

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 virtual Department Departments { get; set; }

    }
 public class Department
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Location { get; set; }
        public virtual ICollection<Employee> Employees { get; set; }

        public Department()
        {
            this.Employees = new List<Employee>();
        }

    }

Initializer

public class DepartmentInitializer : DropCreateDatabaseIfModelChanges<EmployeeDBContext>
    {
        protected override void Seed(EmployeeDBContext context)
        {
            IList<Department> lst = new List<Department>
            {
                new Department
                {
                    Name = "Developer",
                    Location = "Bangalore"
                },
                new Department
                {
                    Name = "Tester",
                    Location = "Bangalore"
                },
                new Department
                {
                    Name = "IT Services",
                    Location = "Chennai"
                }
            };
            foreach (var item in lst)
            {
                context.Departments.Add(item);
            }
            context.SaveChanges();
        }
    }

Main App

class Program
    {
        static void Main(string[] args)
        {
            using (var db = new EmployeeDBContext())
            {
                Database.SetInitializer<EmployeeDBContext>(new DepartmentInitializer());
            }
        }
    }
MushinNoShin
  • 4,695
  • 2
  • 32
  • 46
S.Sagar
  • 21
  • 8

2 Answers2

0

Try actually querying your db

On my machine, the seeder runs when I query it for the first time.

using (var db = new EmployeeDBContext())
{
    Database.SetInitializer<EmployeeDBContext>(new DepartmentInitializer());
    var depts = db.Departments.ToList();
}
0

For version 6 of Entity Framework, using 'migrations' is the preferred way to version the database, using the "Configuration.Seed" method as shown in this tutorial:

http://www.asp.net/web-api/overview/data/using-web-api-with-entity-framework/part-3

Have you tried running "Update-Database" from the Package Manager Console to get it to work?

I know I have had issues using the older seeding method with EF6. Migrations has also changed for Entity Framework Core 1 (formerly EF7), so make sure you are applying the correct technique to the correct version.

WillC
  • 1,761
  • 17
  • 37
  • i tried the same way and it was working, but why the above approach doesnt work in EF 6. Any idea? – S.Sagar Jul 19 '16 at 17:43
  • I think it is to with the inheritance of the class from the DatabaseContext not calling the seed method correctly. See: http://stackoverflow.com/questions/25524238/seed-method-not-called-entity-framework-6 – WillC Jul 19 '16 at 17:59