Seeding Many-To-Many data
Asked Answered
A

3

3

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.

My database looks like this:

enter image description here

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

My seeder looks like this

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!

Amalgamation answered 6/1, 2016 at 18:50 Comment(0)
J
2

Add a call to

context.SaveChanges();  

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.

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.

Jennajenne answered 6/1, 2016 at 19:6 Comment(1)
Thanks alot for you response, this was a simple fix!Amalgamation
B
2

You look like you are assuming the IDs. If you have them as identity columns in your database that's probably not a great move.

With identity keys, you should call context.SaveChanges() to write to the db and get the ID's back. Then you should use the values from your objects rather than hardcoding ID values.

Backchat answered 6/1, 2016 at 19:2 Comment(1)
Valid question - I concluded that from the fact that there's no explicit setting of IDs in his code.Backchat
J
2

Add a call to

context.SaveChanges();  

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.

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.

Jennajenne answered 6/1, 2016 at 19:6 Comment(1)
Thanks alot for you response, this was a simple fix!Amalgamation
T
0

I am assuming you have navigation properties on Skill entity as follows

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 = "[email protected]"}} },

     );

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

        context.SaveChanges();

      }
}
Tense answered 6/1, 2016 at 19:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.