title: One-to-one relationship in EF Core (The child/dependent side could not be determined for the one-to-one relationship)
body: <p>I've been getting the following error message and I can't seem to grasp the reason why have I been getting it. Interestingly when adding migrations I didn't get any errors, but whenever I want to use the context I do get it.</p>
  
  <blockquote>
    <p>The child/dependent side could not be determined for the one-to-one
    relationship between 'Block.JobBlock' and 'JobBlock.Block'. To
    identify the child/dependent side of the relationship, configure the
    foreign key property. If these navigations should not be part of the
    same relationship configure them without specifying the inverse.</p>
  </blockquote>
  
  <p>A <code>Job</code> can have multiple <code>JobBlocks</code> (one-to-many); single <code>Block</code> can have only one <code>JobBlock</code> (one-to-one). So basically, a <code>JobBlock</code> is a reference table/entity used to reference <code>Job</code> and its <code>Blocks</code>. It is important to mention that a primary key in the <code>JobBlock</code> entity consists of two keys thus making it a compound primary key.</p>
  
  <p>One might argue that a <code>Block</code> entity should already contain an <code>IdJob</code> property and that the <code>JobBlock</code> entity could be entirely dismissed, but there is some reasoning why it shouldn't be done this way so let's leave it as it is :) </p>
  
  <p><strong>Models:</strong></p>
  
  <pre><code>public class Job : IEntity
  {
      public Job()
      {
          JobBlocks = new HashSet&lt;JobBlock&gt;();
      }
  
      public Guid Id { get; set; } = Guid.NewGuid();
      public ICollection&lt;JobBlock&gt; JobBlocks { get; set; }
  }
  
  public class Block : IEntity
  {
      public Guid Id { get; set; } = Guid.NewGuid();
      public JobBlock JobBlock { get; set; }
  }
  
  
  public class JobBlock : IEntity
  {
      public Guid IdJob { get; set; }
      public Job Job { get; set; }
  
      public Guid IdBlock { get; set; }
      public Block Block { get; set; }
  }
  </code></pre>
  
  <p><strong>EF Configurations:</strong></p>
  
  <pre><code>public class JobConfiguration : IEntityTypeConfiguration&lt;Job&gt;
  {
      public void Configure(EntityTypeBuilder&lt;Job&gt; builder)
      {
          builder.HasKey(p =&gt; p.Id);
          builder.Property(p =&gt; p.Id) .IsRequired() .ValueGeneratedNever();
  
          builder.HasMany(e =&gt; e.JobBlocks)
              .WithOne(e =&gt; e.Job)
              .HasForeignKey(p =&gt; p.IdJob);
      }
  }
  
  public class BlockConfiguration : IEntityTypeConfiguration&lt;Block&gt;
  {
      public void Configure(EntityTypeBuilder&lt;Block&gt; builder)
      {
          builder.HasKey(p =&gt; p.Id);
          builder.Property(p =&gt; p.Id).IsRequired().ValueGeneratedNever();
  
          builder.HasOne(e =&gt; e.JobBlock)
              .WithOne(e =&gt; e.Block)
              .HasForeignKey&lt;JobBlock&gt;(p =&gt; new { p.IdJob, p.IdBlock });
      }
  }
  
  public class JobBlockConfiguration : IEntityTypeConfiguration&lt;JobBlock&gt;
  {
      public void Configure(EntityTypeBuilder&lt;JobBlock&gt; builder)
      {
          builder.HasKey(p =&gt; new { p.IdJob, p.IdBlock });
          builder.Property(p =&gt; p.IdJob).IsRequired();
          builder.Property(p =&gt; p.IdBlock).IsRequired();
  
          builder.HasOne(e =&gt; e.Job)
              .WithMany(e =&gt; e.JobBlocks)
              .HasForeignKey(p =&gt; p.IdJob);
  
          builder.HasOne(e =&gt; e.Block)
              .WithOne(e =&gt; e.JobBlock)
              .HasForeignKey&lt;JobBlock&gt;(p =&gt; new { p.IdJob, p.IdBlock });
      }
  }
  </code></pre>