title: Table Per Hierarchy - Foreign keys on base and derived
body: <p>I'm not sure if this was possible in EF 6 or earlier. (Current version I use is EF Core 3.1.4) What I'm trying to achieve is to have a base entitiy with some properties and a connection to another entity. From the base entity to have 2 other entities with different connections (foreign keys or navigation properties) to other entities. Is this possible in any way or should I abandon the idea?</p>
  
  <p>The error I get is:</p>
  
  <blockquote>
    <p>A key cannot be configured on 'DerivedResource1' because it is a derived type. The key must be configured on the root type 'Resource'. If you did not intend for 'Resource' to be included in the model, ensure that it is not included in a DbSet property on your context, referenced in a configuration call to ModelBuilder, or referenced from a navigation property on a type that is included in the model.</p>
  </blockquote>
  
  <p>And the code:</p>
  
  <pre><code>public class Resource : BaseEntity
  {
      public string Name { get; set; }
      public string Description { get; set; }
      public ICollection&lt;Connection1&gt; Connection1 { get; set; }
  }
  
     public class DerivedResource1 : Resource
  {
      public int NrOfSeats { get; set; }
      public int Capacity { get; set; }
      public Guid Connection2Id { get; set; }
      public Connection2 Connection2 { get; set; }
  }
  
      public class DerivedResource2 : Resource
  {
      public string Title { get; set; }
      public string Text { get; set; }
      public ICollection&lt;ManyToManyResource&gt; ManyToManyResources { get; set; }
  }
  </code></pre>
  
  <p>And the configurations looks like this:</p>
  
  <pre><code> private void ConfigureResource(EntityTypeBuilder&lt;Resource&gt; config)
      {
          config.ToTable("Resources")
              .HasDiscriminator&lt;int&gt;("ResourceType")
              .HasValue&lt;DerivedResource1&gt;(1)
              .HasValue&lt;DerivedResource2&gt;(2);
      }
  
      private void ConfigureDerivedResource1(EntityTypeBuilder&lt;DerivedResource1&gt; config)
      {
          config.HasOne(x =&gt; x.Connection2)
              .WithMany(x =&gt; x.DerivedResource1)
              .HasForeignKey(x =&gt; x.Connection2Id);
      }
  
  protected override void OnModelCreating(ModelBuilder builder)
      {
          base.OnModelCreating(builder);
          builder.Entity&lt;Connection1&gt;(config =&gt; ConfigureEntity(config, ConfigureConnection1));
          builder.Entity&lt;DerivedResource1&gt;(config =&gt; ConfigureEntity(config, ConfigureDerivedResource1));
          builder.Entity&lt;Connection2&gt;();
          builder.Entity&lt;DerivedResource2&gt;();
          builder.Entity&lt;Resource&gt;(config =&gt; ConfigureEntity(config, ConfigureResource));
          builder.Entity&lt;ManyToMany&gt;();
          builder.Entity&lt;ManyToManyResource&gt;(config =&gt; ConfigureEntity(config, ConfigureManyToManyResource));
      }
  </code></pre>