播种多对多数据 [英] Seeding Many-To-Many data

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

问题描述

您好,我正在尝试为我的项目创建一些种子,但是在将多对多关系数据播种到数据库时遇到了麻烦.

Hello I'm trying to create some seeds for my project, but I'm having trouble seeding the Many-To-Many relationship data to the db.

我的数据库如下:

Teacher_ID Skill_ID 是他们课程表的外键.

in TeacherSkills, Teacher_ID and Skill_ID are foreign keys for their tables ofcourse.

我的种子看起来像这样

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

    }

老师和技能播种没有问题,但是我不能给老师播种任何技能.

The teacher and skills seed without any problem, but I can't seed any skills to a teacher.

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

It doesn't give me any errors and I'm not sure what wrong.

希望大家能帮助您,谢谢!

I hope any of you can help, thanks!

推荐答案

添加呼叫

context.SaveChanges();  

在您填补教师的位置和您将要播授的教师技能之间的位置.这应该提交技能和教师"集合,以便您对键列(标识值)的假设将被INSERT执行时实际生成的假设所取代.

between where you filled the teachers and where you are going to seed the teacherskills. This should commit the Skills and Teachers collections so your assumptions about the key columns (identity values) will be replaced by those actually generated when the INSERTs execute.

此外,可以检索实际的教师和技能的ID值,并将其用于TeacherSkills构造函数中,以消除种子代码中的魔术数字" 1-4.

Furthermore the actual teachers' and skills' ID values can be retrieved and used in the TeacherSkills constructors to get rid of the "magic numbers" 1-4 in the Seed code.

这篇关于播种多对多数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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