title: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_PostTag_Tag_TagId"
body: <p>I am using <strong>Entity Framework 7 RC1</strong> and I have the entities:</p>
  
  <pre><code>public class Post {
    public Int32 Id { get; set; }
    public String Title { get; set; }
    public virtual IList&lt;PostTag&gt; PostsTags { get; set; }
  }
  
  public class Tag {
    public Int32 Id { get; set; }
    public String Name { get; set; }
    public virtual IList&lt;PostTag&gt; PostsTags { get; set; }
  }
  
  public class PostTag {
    public Int32 PostId { get; set; }
    public Int32 TagId { get; set; }
    public virtual Post Post { get; set; }
    public virtual Tag Tag { get; set; }
  }
  </code></pre>
  
  <p>The model configuration for these entities is the following:</p>
  
  <pre><code>protected override void OnModelCreating(ModelBuilder builder) {
  
    base.OnModelCreating(builder);
  
    builder.Entity&lt;Post&gt;(b =&gt; {
  
      b.ToTable("Posts");
      b.HasKey(x =&gt; x.Id);
      b.Property(x =&gt; x.Id).UseSqlServerIdentityColumn();
      b.Property(x =&gt; x.Title).IsRequired().HasMaxLength(100);
    });
  
    builder.Entity&lt;Tag&gt;(b =&gt; {
      b.ToTable("Tags");
      b.HasKey(x =&gt; x.Id);
      b.Property(x =&gt; x.Id).UseSqlServerIdentityColumn();
      b.Property(x =&gt; x.Name).IsRequired().HasMaxLength(100);
    });
  
    builder.Entity&lt;PostTag&gt;(b =&gt; {
      b.ToTable("PostsTags");
      b.HasKey(x =&gt; new { x.PostId, x.TagId });
      b.HasOne(x =&gt; x.Post).WithMany(x =&gt; x.PostsTags).HasForeignKey(x =&gt; x.PostId);
      b.HasOne(x =&gt; x.Tag).WithMany(x =&gt; x.PostsTags).HasForeignKey(x =&gt; x.TagId);
    });
  
  }
  </code></pre>
  
  <p>I created the migration and the database. Then I tried to create a post:</p>
  
  <pre><code>  Context context = new Context();
  
    Post post = new Post {
      PostsTags = new List&lt;PostTag&gt; {
        new PostTag {
          Tag = new Tag { Name = "Tag name" }
        }
      },
      Title = "Post title"
    };
  
    context.Posts.Add(post);
  
    await _context.SaveChangesAsync();
  </code></pre>
  
  <p>And on save I get the following error:</p>
  
  <pre><code>An error occurred while updating the entries. 
  The INSERT statement conflicted with the FOREIGN KEY constraint "FK_PostTag_Tag_TagId". 
  The conflict occurred in database "TestDb", table "dbo.Tags", column 'Id'. 
  The statement has been terminated.
  </code></pre>
  
  <p>Does anyone knows the reason for this error?</p>