播种多对多数据
Seeding Many-To-Many data
您好,我正在尝试为我的项目创建一些种子,但我在将多对多关系数据播种到数据库时遇到问题。
我的数据库看起来像这样:
在 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,
});
}
}
老师和技能种子没有问题,但我无法将任何技能种子给老师。
它没有给我任何错误,我不确定哪里出了问题。
希望大家帮帮忙,谢谢!
你看起来像是在假设这些 ID。如果您将它们作为数据库中的标识列,那可能不是一个很好的举措。
使用身份密钥,您应该调用 context.SaveChanges() 写入数据库并取回 ID。那么您应该使用对象中的值而不是硬编码 ID 值。
添加呼叫
context.SaveChanges();
在你培养教师的地方和你要培养教师技能的地方之间。这应该提交 Skills 和 Teachers 集合,这样您对键列(标识值)的假设将被 INSERT 执行时实际生成的那些所取代。
此外,可以检索实际教师和技能的 ID 值并在 TeacherSkills 构造函数中使用,以摆脱种子代码中的 "magic numbers" 1-4。
我假设您在 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();
}
}
您好,我正在尝试为我的项目创建一些种子,但我在将多对多关系数据播种到数据库时遇到问题。
我的数据库看起来像这样:
在 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,
});
}
}
老师和技能种子没有问题,但我无法将任何技能种子给老师。
它没有给我任何错误,我不确定哪里出了问题。
希望大家帮帮忙,谢谢!
你看起来像是在假设这些 ID。如果您将它们作为数据库中的标识列,那可能不是一个很好的举措。
使用身份密钥,您应该调用 context.SaveChanges() 写入数据库并取回 ID。那么您应该使用对象中的值而不是硬编码 ID 值。
添加呼叫
context.SaveChanges();
在你培养教师的地方和你要培养教师技能的地方之间。这应该提交 Skills 和 Teachers 集合,这样您对键列(标识值)的假设将被 INSERT 执行时实际生成的那些所取代。
此外,可以检索实际教师和技能的 ID 值并在 TeacherSkills 构造函数中使用,以摆脱种子代码中的 "magic numbers" 1-4。
我假设您在 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();
}
}