2013-03-26 14 views
5

Ho il seguente modello semplice che viene implementato nell'approccio Code First. Dipartimento e corsi hanno uno o più rapporti. Un dipartimento può avere molti corsi mentre un corso può appartenere esattamente a un dipartimento. Ecco il modelloSemina di relazioni uno a molti nel codice

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; } 

}

Il mio problema è che voglio sementi di loro. Voglio almeno 5 valori nella mia funzione Seed. Ecco la funzione 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(); 

ma questo non funziona. Sono nuovo di EF e Code First ... e le scadenze future ... Qualcuno può aiutarmi per favore come è il modo corretto di Seminare il DB.

+0

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

risposta

0

Dal momento che si sta generando le chiavi primarie da soli, è necessario mettere [DatabaseGenerated(DatabaseGenerationOption.None)] attributo per CourseID e IDdipartimento in modo che Entity Framework saprà che si sta generando le chiavi.

Una soluzione migliore sarebbe quella di mantenere basandosi sul EntityFramework per le chiavi primarie, ma per cambiare un po 'il metodo di

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)); 

In che causano e la necessità di salvare i corsi e dal momento che si sta associando il Dipartimento , verrà salvato implicitamente.

Buona fortuna con la tua scadenza!

3

Non impostare le chiavi primarie nel metodo Seed(). Entity Framework saprà che le proprietà con Id nel nome saranno chiavi primarie.

Prova quanto segue nel Seed() metodo:

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

Hai provato a utilizzare il codice sopra? Ha risolto il tuo problema? – MattSull

+0

Stavo avendo lo stesso problema e questo lavoro mi ha affascinato. – tonyedwardspz

+0

la tua risposta mi ha aiutato ma per favore vedi la mia risposta qui sotto per una soluzione più semplice –

0

Ecco come lo faccio, è sufficiente per inizializzare i 'bambini' prima del 'genitore', non SaveChanges() necessaria:

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 } 
    ); 
Problemi correlati