Mam następujący prosty model, który jest wdrażany w ramach podejścia Code First. Departament i kursy mają jedno do wielu relacji. Wydział może mieć wiele kursów, podczas gdy kurs może należeć do dokładnie jednego działu. Oto model.Wysiewanie relacji jeden do wielu w kodzie pierwszym
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; }
}
Mój problem jest chcę ich nasion. Chcę mieć co najmniej 5 wartości w mojej funkcji Seed. Oto funkcja Seed.
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();
ale to nie działa. Jestem nowy w EF i Code First ... i terminach do przodu ... Czy ktoś może mi pomóc proszę, jak to jest właściwy sposób wysiewu DB.
http://ehsanghanbari.com/Post/6/getting-started-with-entity-framework – Ehsan