2013-03-26 12 views
5

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.

+0

http://ehsanghanbari.com/Post/6/getting-started-with-entity-framework – Ehsan

Odpowiedz

0

Ponieważ jesteś generowania kluczy podstawowych siebie, trzeba umieścić [DatabaseGenerated(DatabaseGenerationOption.None)] atrybut dla CourseId i DepartmentId tak że Entity Framework będzie wiedział, że jesteś generowania kluczy.

Lepszym rozwiązaniem byłoby utrzymanie powołując się na EntityFramework dla kluczy głównych, ale nieznacznie zmienić metodę

var courses = new List<Course> 
     { 
      new Course {Title = "Classic English", Description = "Some  Description", Department= 
    new Department { Title = "English", Description ="English  
Department", Courses = new List<Course>() }}, 
      ..... 
     }; 
     courses.ForEach(t => context.Courses.Add(t)); 

W tej przyczyny was i trzeba zapisać na kursy i ponieważ jesteś kojarzenie Departament zostanie zapisany niejawnie.

Życzymy powodzenia z terminem!

3

Nie ustawiaj kluczy podstawowych w metodzie Seed(). Entity Framework będzie wiedział, że właściwości o identyfikatorze w nazwie będą kluczami podstawowymi.

Spróbuj wykonać następujące czynności w sposób Seed():

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(); 
} 
+0

Czy próbowałeś użyć powyższego kodu? Czy to rozwiązało Twój problem? – MattSull

+0

Miałem ten sam problem, a ta praca była dla mnie urokiem. – tonyedwardspz

+0

Twoja odpowiedź pomogła mi, ale zapoznaj się z poniższą odpowiedzią, aby uzyskać prostsze rozwiązanie –

0

Oto jak to zrobić, wystarczy zainicjować „dzieci” przed „rodzica”, nie SaveChanges() potrzebne:

var choices = new List<Choices> 
{ 
    new Choices { Description = "Choice1" }, 
    new Choices { Description = "Choice2" }, 
    new Choices { Description = "Choice3" }, 
    new Choices { Description = "Choice4" } 
}; 

choices.ForEach(c => context.Choices.AddOrUpdate(c)); 

context.Questions.AddOrUpdate(
    p => p.ID, 
    new Question { QuestionText = "Question ?", Choices = choices } 
    ); 
Powiązane problemy