Code First中的一对多关系播种

Cur*_*ous 5 entity-framework

我有以下简单模型,正在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; }
Run Code Online (Sandbox Code Playgroud)

}

我的问题是我想种下它们.我想在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();
Run Code Online (Sandbox Code Playgroud)

但这不起作用.我是EF和Code First的新手......以及未来的截止日期......任何人都可以帮助我,因为有什么方法可以播放数据库.

Bis*_* C. 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));
Run Code Online (Sandbox Code Playgroud)

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

祝你在截止日期前好运!