播种多对多数据

时间:2016-01-06 18:50:20

标签: c# asp.net-mvc entity-framework code-first seeding

您好我试图为我的项目创建一些种子,但是我无法将多对多关系数据播种到数据库。

我的数据库如下所示:

enter image description here

TeacherSkills 中的

Teacher_ID Skill_ID 是其课程表的外键。

我的播种机看起来像这样

protected override void Seed(Ability_Examen_ASP.Models.AbilityDbContext context)
    {
        if (!context.Skills.Any())
        {
            context.Skills.Add(new Models.Skill { SkillName = "PHP" });
            context.Skills.Add(new Models.Skill { SkillName = "Java" });
            context.Skills.Add(new Models.Skill { SkillName = "Frans" });
            context.Skills.Add(new Models.Skill { SkillName = "Drupal" });
        }

        if (!context.Teachers.Any())
        {
            context.Teachers.Add(new Models.Teacher
            {
                FirstName = "Joris",
                LastName = "Hens",
                Campus = "Kruidtuin",
                Password = "testpass",
                Email = "Joris.Hens@"
            });
            context.Teachers.Add(new Models.Teacher
            {
                FirstName = "Anne",
                LastName = "Van Goetem",
                Campus = "Kruidtuin",
                Password = "testpass",
                Email = "Anne.Vangoetem@"
            });
            context.Teachers.Add(new Models.Teacher
            {
                FirstName = "Sofie",
                LastName = "Krekels",
                Campus = "De Ham",
                Password = "testpass",
                Email = "Sofie.Krekels@"
            });
            context.Teachers.Add(new Models.Teacher
            {
                FirstName = "Robby",
                LastName = "Vanelderen",
                Campus = "De Vest",
                Password = "testpass",
                Email = "Robby.Vanelderen@"
            });
        }

        if (!context.TeacherSkills.Any())
        {
            context.TeacherSkills.Add(new Models.TeacherSkill
            {
                Teacher_ID = 1,
                Skill_ID = 1,
            });
            context.TeacherSkills.Add(new Models.TeacherSkill
            {
                Teacher_ID = 1,
                Skill_ID = 4,
            });
            context.TeacherSkills.Add(new Models.TeacherSkill
            {
                Teacher_ID = 2,
                Skill_ID = 2,
            });
            context.TeacherSkills.Add(new Models.TeacherSkill
            {
                Teacher_ID = 3,
                Skill_ID = 3,
            });
            context.TeacherSkills.Add(new Models.TeacherSkill
            {
                Teacher_ID = 4,
                Skill_ID = 4,
            });
        }

    }

老师和技能种子没有任何问题,但我不能将任何技能传播给老师。

它没有给我任何错误,我不确定是什么错误。

我希望你们中的任何人都能提供帮助,谢谢!

3 个答案:

答案 0 :(得分:2)

你看起来像是在假设身份证。如果你将它们作为数据库中的标识列,这可能不是一个好的举动。

使用身份密钥,您应该调用context.SaveChanges()来写入数据库并获取ID。然后,您应该使用对象中的值而不是硬编码ID值。

答案 1 :(得分:2)

添加对

的调用
context.SaveChanges();  
在你填补教师的地方和你要在哪里教授教师技能之间。这应该提交技能和教师集合,因此您对关键列(标识值)的假设将被INSERT执行时实际生成的假设所取代。

此外,可以在TeacherSkills构造函数中检索并使用实际的教师和技能ID值,以消除种子代码中的“幻数”1-4。

答案 2 :(得分:0)

我假设您在Skill实体上有导航属性,如下所示

public class Skill{
 public string SkillName{get; set;}
 public virtual IList<Teacher> Teachers{get;set;}
}


protected override void Seed(Ability_Examen_ASP.Models.AbilityDbContext context){
 if (!context.Skills.Any())
    {


        if(!context.Skills.Any() && !context.Teachers.Any()){
            context.Skills.Add(new Models.Skill{ SkillName = "PHP", Teachers = new List<Teacher>{new Teacher{FirstName = "Joris",
            LastName = "Hens",
            Campus = "Kruidtuin",
            Password = "testpass",
            Email = "Joris.Hens@thomasmore.be"}} },

     );

 // repeat for other skills followed by list of teachers
          }

        context.SaveChanges();

      }
}