title: EF Core Fluent API Null Foreign Key
body: <p>I have the following tables : Products, Users and ProductApproval. Whenever user create a new product, the ProductApproval will have a new record with the product ID and a null ApprovedByUserId because it is not approve yet. User can have many ProductApproval but Product only can have one ProductApproval.</p>
  
  <p>The structure is like this:</p>
  
  <p>User.cs </p>
  
  <pre><code>public class User
  {
      public int Id { get; set; }
      public string Username { get; set; }
  
      public virtual ICollection&lt;ProductApproval&gt; ProductApprovals { get; set; }
  
      public User()
      {
         ProductApprovals = new Collection&lt;ProductApproval&gt;();
      }
  }
  </code></pre>
  
  <p>Product.cs </p>
  
  <pre><code>public class Product
  {
      public int Id { get; set; }
      public string Name { get; set; }
  
      public virtual ProductApproval ProductApproval { get; set; }
  }
  </code></pre>
  
  <p>ProductApproval.cs</p>
  
  <pre><code>public class ProductApproval
  {
     public int Id { get; set; }
  
     public virtual Product Product { get; set; }
     public int? ProductId { get; set; }
  
     public virtual User User { get ;set; }
     public int? ApprovedByUserId { get; set; }
  
  }
  </code></pre>
  
  <p>DataContext.cs </p>
  
  <pre><code>modelBuilder.Entity&lt;User&gt;()
     .HasMany(u =&gt; u.ProductApprovals)
     .WithOne(pa =&gt; pa.User)
     .HasForeignKey(pa =&gt; pa.ApprovedByUserId)
     .IsRequired(false);
  
  modelBuilder.Entity&lt;Product&gt;()
     .HasOne(p =&gt; p.ProductApproval)
     .WithOne(pa =&gt; pa.Product)
     .HasForeignKey&lt;ProductApproval&gt;(pa =&gt; pa.ProductId);
  </code></pre>
  
  <p>I already declared the foreign key ApprovedByUserId as nullable, but i still get the following error when i insert a record into productapprovals : 
  <strong>MySqlException: Cannot add or update a child row: a foreign key constraint fails (<code>mysite</code>.<code>productapprovals</code>, CONSTRAINT <code>FK_ProductApprovals_Users_ApprovedByUserId</code> FOREIGN KEY (<code>ApprovedByUserId</code>) REFERENCES <code>users</code> (<code>Id</code>) ON DELETE RESTRICT)</strong></p>
  
  <p>Please advice is there any place i did wrong</p>