I have got the following simple model which is being implemented in Code First approach. Department and Courses have one to many relationship. A department can have many courses while a course can belong to exactly one department. Here is the model.
public class Department
{
public int DepartmentId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public virtual ICollection<Course> Courses { get; set; }
}
public class Course
{
public int CourseId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public int DepartmentId { get; set; }
public virtual Department Department { get; set; }
}
My problem is I want to Seed them. I want at least 5 values in my Seed function. Here is the Seed function.
public class DataInitializer : DropCreateDatabaseIfModelChanges<StudentRecordContext>
{
protected override void Seed(StudentRecordContext context)
{
var departments = new List<Department>
{
new Department { DepartmentId = 1, Title = "English", Description ="English Department", Courses = new List<Course>() },
new Department { DepartmentId= 2,Title = "Chemistry", Description ="chemistry department", Courses = new List<Course>() },
new Department { DepartmentId= 3,Title = "Mahematics", Description ="mathematics department", Courses = new List<Course>() },
new Department { DepartmentId= 4,Title = "Philosophy", Description ="philosophy department", Courses = new List<Course>() },
new Department { DepartmentId= 5,Title = "Biology", Description ="biology department", Courses = new List<Course>() }
};
departments.ForEach( t => context.Departments.Add(t));
context.SaveChanges();
var courses = new List<Course>
{
new Course { CourseId = 1055, Title = "Classic English", Description = "Some Description", DepartmentId = 1 },
new Course { CourseId = 2055, Title = "Applied Chemistry", Description = "Some Description", DepartmentId = 2 },
new Course { CourseId = 2056, Title = "Applied Mathematics", Description = "Some Description", DepartmentId = 3 },
new Course { CourseId = 3041, Title = "MetaPhysics", Description = "Some Description", DepartmentId = 4 },
new Course { CourseId = 3024, Title = "Molecular Biology", Description = "Some Description", DepartmentId = 5 },
};
courses.ForEach(t => context.Courses.Add(t));
context.SaveChanges();
but this does not work. I am new to EF and Code First... and deadlines ahead... Can anyone help me please as whats the correct way of Seeding the DB.
Don't set primary keys in the Seed()
method. Entity Framework will know that properties with Id in the name will be primary keys.
Try the following in your Seed()
method:
protected override void Seed(StudentRecordContext context)
{
var departments = new List<Department>
{
// this will have DepartmentId = 1
new Department { Title = "English", Description ="English Department", Courses = new List<Course>() },
// more departments
};
departments.ForEach(d => context.Departments.Add(d));
context.SaveChanges();
var courses = new List<Course>
{
// this will have CourseId = 1
new Course { Title = "Classic English", Description = "Some Description", Department = departments.FirstOrDefault(d => d.DepartmentId == 1) },
// this will have CourseId = 2
new Course { Title = "Drama", Description = "Some Description", Department = departments.FirstOrDefault(d => d.DepartmentId == 1) },
// both of the above courses will be added to Department with DepartmentId = 1
// more courses
};
courses.ForEach(c => context.Courses.Add(c));
context.SaveChanges();
// now add the two courses to the department
departments[0].Courses.Add(courses[0]); // in list departments at index 0 add course from list courses at index 0
departments[0].Courses.Add(courses[1]); // in list departments at index 0 add course from list courses at index 1
context.SaveChanges();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With