2013-03-26 77 views
5

我已经得到了以Code First方法实施的以下简单模型。系和课程有一对多的关系。一个部门可以有很多课程,而一门课程只能属于一个部门。这是模型。代码优先播种的一对多关系

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

}

我的问题是我想他们种子。我希望Seed函数中至少有5个值。这里是种子功能。

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

但这不起作用。我是EF和Code First的新手......以及未来的最后期限......任何人都可以帮助我,因为播种DB的正确方式是什么。

+0

http://ehsanghanbari.com/Post/6/getting-started-with-entity-framework – Ehsan 2014-02-26 13:07:47

回答

0

因为您自己生成主键, 你需要把 [DatabaseGenerated(DatabaseGenerationOption.None)] 属性为CourseId和DepartmentID的,使实体框架会知道你正在生成的密钥。

一个更好的选择是继续依靠的EntityFramework的主键,但是你稍微改变你的方法

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

在这种原因,需要保存的课程,因为你是关联部,它将被隐式保存。

祝你好运与你的截止日期!

3

不要在Seed()方法中设置主键。实体框架将知道名称中带有Id的属性将是主键。

尝试在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

您是否尝试使用上述代码?它解决了你的问题吗? – MattSull 2013-03-31 17:49:40

+0

我有同样的问题,这项工作像我的魅力。 – tonyedwardspz 2015-01-06 02:20:52

+0

你的回答对我有帮助,但请看下面的答案以获得更简单的解决方案 – 2016-04-12 16:47:18

0

这里是我如何做到这一点,你只需要在“父”之前初始化“孩子”,没有调用SaveChanges()需要:

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