当我播种数据库时,我正在观察奇怪的行为。我有三个班:
User
,Skill
和SkillSet
,我将其用作模型来创建表明以下内容的关系:
1 User
有1 SkillSet
1 SkillSet
可以包含多个Skills
。
在我的种子方法中,当我需要在表格中填写技能组与用户ID的关系时,我称之为:
Skill Skill1 = new Skill { Name = "Programming", "C++" };
Skill Skill2 = new Skill { Name = "Programming", "Java"};
Skill Skill3 = new Skill { Name = "Programming", "PHP"};
Skill Skill4 = new Skill { Name = "Programming", "Javascript" };
context.Skills.AddOrUpdate(Skill1,Skill2,Skill3,Skill4);
SkillSet SkillSet1 = new SkillSet();
SkillSet1.UserId = 1;
SkillSet1.Skills.Add(Skill1);
SkillSet1.Skills.Add(Skill2);
SkillSet SkillSet2 = new SkillSet();
SkillSet2.UserId = 2;
SkillSet2.Skills.Add(Skill2);
SkillSet2.Skills.Add(Skill3);
SkillSet SkillSet3 = new SkillSet();
SkillSet3.UserId = 3;
SkillSet3.Skills.Add(Skill1);
SkillSet3.Skills.Add(Skill4);
context.SkillSets.AddOrUpdate(SkillSet1, SkillSet2,SkillSet3);
以下是Skill
和SkillSet
类
public class Skill
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
//Platform
public string TechnologyName { get; set; }
}
public class SkillSet
{
public SkillSet()
{
Skills = new List<Skill>();
}
public int Id { get; set; }
[Required]
public int UserId { get; set; }
[Required]
public virtual ICollection<Skill> Skills { get; set; }
}
问题是我更新数据库并调用种子方法后,
SkillSet
和User
之间的关系表看起来非常奇怪,并且反转了Id&#39; s。
这是一个截图:
有人可以解释为什么ID会被倒置吗?