实体框架

时间:2016-06-11 12:59:13

标签: c# entity-framework

当我播种数据库时,我正在观察奇怪的行为。我有三个班: UserSkillSkillSet,我将其用作模型来创建表明以下内容的关系:

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

以下是SkillSkillSet

的代码
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; }
}

问题是我更新数据库并调用种子方法后, SkillSetUser之间的关系表看起来非常奇怪,并且反转了Id&#39; s。 这是一个截图: enter image description here

有人可以解释为什么ID会被倒置吗?

0 个答案:

没有答案