Seeding Many-To-Many data

元气小坏坏 提交于 2021-02-09 07:13:25

问题


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:

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!


回答1:


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.




回答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.




回答3:


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 = "Joris.Hens@thomasmore.be"}} },

     );

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

        context.SaveChanges();

      }
}


来源:https://stackoverflow.com/questions/34640542/seeding-many-to-many-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!