一对多关系种子在代码第一 [英] One-to-Many Relationship Seeding in Code First

查看:66
本文介绍了一对多关系种子在代码第一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下简单的模式,正在Code First方法中实现。部门和课程有一对多的关系。一个部门可以有许多课程,而课程可以属于一个部门。这是模型。

  public class Department 
{
public int DepartmentId {get;组; }
public string标题{get;组; }
public string描述{get;组; }
public virtual ICollection< Course>课程{get;组; }
}

public class课程
{
public int CourseId {get;组; }
public string标题{get;组; }
public string描述{get;组; }
public int DepartmentId {get;组; }
public virtual Department Department {get;组;

}



我的问题是我想种子他们我想要在我的Seed功能中至少有5个值。这是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 =化学系,课程=新列表&课程>()},
new Department {DepartmentId = 3,Title =Mahematics,Description =数学系 = new List< Course>()},
new Department {DepartmentId = 4,Title =Philosophy,Description =哲学系,课程=新列表&课程>()},
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列表<课程>
{
new Course {CourseId = 1055,Title =Classic English,Description =Some Description,DepartmentId = 1},
new Course {CourseId = 2055,Title =Applied化学,Description =Some Description,DepartmentId = 2},
new Course {CourseId = 2056,Title =Applied Mathematics,Description =Some Description,DepartmentId = 3},
new课程{CourseId = 3041,Title =MetaPhysics,Description =Some Description,DepartmentId = 4},
new Course {CourseId = 3024,Title =Molecular Biology,Description =Some Description = 5},
};
courses.ForEach(t => context.Courses.Add(t));
context.SaveChanges();

但这不行。我是新来的EF和代码第一...和最后期限...任何人可以帮助我,正确的方式播种数据库。

解决方案

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



在您的中尝试以下内容Seed()方法:

  protected override void Seed(StudentRecordContext context)
{
var departments = new List< ;系>
{
//这将有DepartmentId = 1
new Department {Title =English,Description =English Department,Courses = new List< Course>()},
//更多部门
};
departments.ForEach(d => context.Departments.Add(d));
context.SaveChanges();

var courses = new列表<课程>
{
//这将有CourseId = 1
new Course {Title =Classic English,Description =Some Description,Department = departments.FirstOrDefault(d => d。 DepartmentId == 1)},
//这将有CourseId = 2
new Course {Title =Drama,Description =Some Description,Department = departments.FirstOrDefault(d => d .DepartmentId == 1)},
//以上两个课程将被添加到DepartmentId = 1
//更多课程
};
courses.ForEach(c => context.Courses.Add(c));
context.SaveChanges();

//现在将两个课程添加到部门
department [0] .Courses.Add(courses [0]); //在索引0的列表部门从索引0的列表课程添加课程
department [0] .Courses.Add(courses [1]); //在索引0的列表部门从索引1的列表课程添加课程
context.SaveChanges();
}


I have got the following simple model which is being implemented in Code First approach. Department and Courses have one to many relationship. A department can have many courses while a course can belong to exactly one department. Here is the model.

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

}

My problem is I want to Seed them. I want at least 5 values in my Seed function. Here is the Seed function.

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

but this does not work. I am new to EF and Code First... and deadlines ahead... Can anyone help me please as whats the correct way of Seeding the DB.

解决方案

Don't set primary keys in the Seed() method. Entity Framework will know that properties with Id in the name will be primary keys.

Try the following in your Seed() method:

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

这篇关于一对多关系种子在代码第一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆