title: Error : Cannot add or update a child row: a foreign key constraint fails
body: <p>I'm still learning EF Core and i Hit an error when trying to insert only User Data (Without BusinessUserProfile and BusinessCompany) into database. I have the following tables : User, BusinessUserProfile and BusinessCompany. The relationship is User has one BusinessUserProfile, BusinessCompany has multiple BusinessUserProfile.</p>
  
  <p>My classes are created like this: </p>
  
  <p>User Model Class :</p>
  
  <pre><code>public class User
  {
      public int Id { get; set; }
      public string Username { get; set; }
  
      public BusinessUserProfile BusinessUserProfile { get; set; }
  
      public User()
      {
          BusinessUserProfile = new BusinessUserProfile();
      }
  }
  </code></pre>
  
  <p>BusinessUserProfile Model Class : </p>
  
  <pre><code>public class BusinessUserProfile
  {
      public int Id { get; set; }
      public string FirstName { get; set; }
      public string LastName { get; set; }
  
      public BusinessCompany BusinessCompany { get; set; }
      public int BusinessCompanyId { get; set; }
  
      public User User { get; set; }
      public int UserId { get; set; }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>BusinessCompany Model Class : </p>
  
  <pre><code>public class BusinessCompany
  {
      public int Id { get; set; }
      public string Name { get; set; }
  
      public ICollection&lt;BusinessUserProfile&gt; BusinessUserProfiles { get; set; }
  
      public BusinessCompany()
      {
          BusinessUserProfiles = new Collection&lt;BusinessUserProfile&gt;();
      }
  }
  </code></pre>
  
  <p>DataContext.cs :</p>
  
  <pre><code>modelBuilder.Entity&lt;User&gt;()
              .HasOne(u =&gt; u.BusinessUserProfile)
              .WithOne(bup =&gt; bup.User)
              .HasForeignKey&lt;BusinessUserProfile&gt;(bup =&gt; bup.UserId);
  
  modelBuilder.Entity&lt;BusinessCompany&gt;()
              .HasMany(bc =&gt; bc.BusinessUserProfiles)
              .WithOne(bup =&gt; bup.BusinessCompany)
              .HasForeignKey(bup =&gt; bup.BusinessCompanyId);
  </code></pre>
  
  <p>When i add an User to the database, i got an error : 
  MySqlException: Cannot add or update a child row: a foreign key constraint fails (<code>mydb</code>.<code>businessuserprofiles</code>, CONSTRAINT <code>FK_BusinessUserProfiles_BusinessCompanies_BusinessCompanyId</code> FOREIGN KEY (<code>BusinessCompanyId</code>) REFERENCES <code>businesscompanies</code> (<code>id</code>) ON DELETE C).</p>
  
  <p>Can anyone advise me on this?</p>