Insert constraint failed nFOREIGN KEY constraint failed While Seeding Data
<p>I am trying to Seed some sample Data </p>

<pre><code>public class Condition 
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class Entity 
{
    public int Id { get; set; }
    public string Name { get; set; }

    public int ConditionId { get; set; }
    public virtual Condition Condition { get; set; }
}
</code></pre>

<p>and in my Seed method..</p>

<pre><code> protected override void Seed(AppContext context)
 {
      Condition condition1 = new Condition();
      condition1.Name = "Cond1";
      Entity.Entity newEntity1 = new Entity.Entity();
      newEntity1.Name = "Test1";
      newEntity1.Condition = condition1;
      context.Entities.Add(newEntity1);

      Condition condition2 = new Condition();
      condition2.Name = "Cond2";
      Entity.Entity newEntity2 = new Entity.Entity();
      newEntity2.Name = "Test Entity 2";
      newEntity2.Condition = condition2;
      context.Entities.Add(newEntity2);
      context.SaveChanges();
 }
</code></pre>

<p>I am getting this Exception constraint failed FOREIGN KEY constraint failed, I couldn't figure out what wrong I am doing here. </p>

<p>I tried calling   context.SaveChanges() after first insertion too and it went fine. but the error appreared only after second context.SaveChanges() only. </p>