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 的新手……还有截止日期……谁能帮我看看播种数据库的正确方法是什么。

4

3 回答 3

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();
}
于 2013-03-27T23:50:24.450 回答
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));

因此,您需要保存课程,并且由于您正在关联部门,因此它将被隐式保存。

祝你最后期限好运!

于 2013-03-26T02:34:48.293 回答
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 }
    );  
于 2016-04-12T16:44:14.620 回答