title: EF Core 2: The MERGE statement conflicted with the FOREIGN KEY constraint
body: <p>I have following classes:</p>
  
  <pre><code>public class GSProject
  {
      public int Id { get; set; }
      public ICollection&lt;GSPoint&gt; Points { get; set; }
      public ICollection&lt;GSPlanarSurface&gt; PlanarSurfaces { get; set; }
  }
  
  public class GSPoint
  {
      public int Id { get; set; }
      public int? PlanarSurfaceId { get; set; }
      public GSPlanarSurface PlanarSurface { get; set; }
  }
  
  public class GSPlanarSurface
      {
          public int Id { get; set; }
  
          public ICollection&lt;GSPoint&gt; Points { get; set; }
  
          public int? PxyId { get; set; }
          public GSPoint Pxy { get; set; }
  
          public int? PyId { get; set; }
          public GSPoint Py { get; set; }
  }
  </code></pre>
  
  <p>and context class (OnModelCreating() related content):</p>
  
  <pre><code>modelBuilder.Entity&lt;GSPlanarSurface&gt;()
                  .HasMany(e =&gt; e.Points)
                  .WithOne(e =&gt; e.PlanarSurface)
                  .HasForeignKey(e =&gt; e.PlanarSurfaceId);
  </code></pre>
  
  <p>Then I created ef migration and related migration .cs file content is:</p>
  
  <pre><code>migrationBuilder.CreateTable(
                  name: "GSPoints",
                  columns: table =&gt; new
                  {
                      Id = table.Column&lt;int&gt;(nullable: false)
                          .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
                      PlanarSurfaceId = table.Column&lt;int&gt;(nullable: true),
                  },
                  constraints: table =&gt;
                  {
                      table.PrimaryKey("PK_GSPoints", x =&gt; x.Id);
                      table.ForeignKey(
                          name: "FK_GSPoints_GSPlanarSurfaces_PlanarSurfaceId",
                          column: x =&gt; x.PlanarSurfaceId,
                          principalTable: "GSPlanarSurfaces",
                          principalColumn: "Id",
                          onDelete: ReferentialAction.Restrict);
                  });
  </code></pre>
  
  <p>Error:<br/>
  {System.Data.SqlClient.SqlException (0x80131904): The MERGE statement conflicted with the FOREIGN KEY constraint "FK_GSPoints_GSPlanarSurfaces_PlanarSurfaceId". The conflict occurred in database "GSTDb", table "dbo.GSPlanarSurfaces", column 'Id'.
  <br/>
  <br/>
  appears if I try to call:</p>
  
  <pre><code>context.Set&lt;GSProject&gt;().Add(entity);
  await context.SaveChangesAsync();
  </code></pre>
  
  <p>entity is an instance of GSProject with a few GSPoints and without any instance of GSPlanarSurface (entity.PlanarSurfaces Count = 0)</p>
  
  <p><strong>EDIT:</strong><br/>
  and here is a script auto generated in t-sql for the key:</p>
  
  <pre><code>USE [GSTDb]
  GO
  
  ALTER TABLE [dbo].[GSPoints]  WITH CHECK ADD  CONSTRAINT [FK_GSPoints_GSPlanarSurfaces_PlanarSurfaceId] FOREIGN KEY([PlanarSurfaceId])
  REFERENCES [dbo].[GSPlanarSurfaces] ([Id])
  GO
  
  ALTER TABLE [dbo].[GSPoints] CHECK CONSTRAINT [FK_GSPoints_GSPlanarSurfaces_PlanarSurfaceId]
  GO
  </code></pre>
  
  <p><br/>
  I executed simple tsql script:</p>
  
  <pre><code>insert into GSProjects ([Description] ,[Name]) values ('d1', 'proj1')
  insert into GSPoints([Number], [IsSlave] ,[X], [Y], [Z]) values (0, 0, 0, 0, 0)
  insert into GSPoints([GSProjectId], [Number], [IsSlave] ,[X], [Y], [Z]) values (5, 0, 0, 0, 0, 0)
  </code></pre>
  
  <p><br/>
  and there were no problems with insertion. First GSPoint has GSProjectId set to null, second GSPoint has GSProjectId set to non null value (5 here). Both ones have GSPlanarSurfaceId set to null and both were inserted correctly.</p>