title: Entity Framework multitenant shared data architecture: single column, multiple foreign keys
body: <p>I have the following data structure:</p>
  
  <pre><code>//property Notification
  abstract class BindableBase { }
  //base class for all tenant-scoped objects
  abstract class TenantModelBase : BindableBase 
  { 
    int TenantId;
  } 
  
  abstract class Order : TenantModelBase 
  {
     Customer Customer; //works: mapped using TenantId and CustomerId
     Product Product; //again, works with TenantId and ProductId
     string ProductId;
     string CustomerId;
  }
  class Customer: TenantModelBase 
  {
     string CustomerId; 
  }
  
  class Product  : TenantModelBase 
  {
     string ProductId;
  }
  
  class SpecialOrder : Order
  {
      OtherClass OtherClass; //this fails!, see below
      string OtherClassId;
  }
  class SuperSpecialOrder : SpecialOrder {  }
  
  class OtherClass  : TenantModelBase 
  {
      string OtherClassId;
  }
  </code></pre>
  
  <p>I get the following error:</p>
  
  <blockquote>
    <p>The foreign key component 'TenantId' is not a declared property on
    type 'SpecialOrder'. Verify that it has not been explicitly excluded
    from the model and that it is a valid primitive property.</p>
  </blockquote>
  
  <p>Error occurs using the Fluent Api Configuration:</p>
  
  <pre><code>        config.HasRequired(p =&gt; p.OtherClass)
              .WithMany(oc =&gt; oc.SpecialOrders)
              .HasForeignKey(p =&gt; new {  p.TenantId, p.OtherClassId});
  </code></pre>
  
  <p>Without the <code>OtherClass</code> reference in <code>SpecialOrder</code> I can create objects freely without problems (including <code>SpecialOrder</code>, <code>SuperSpecialOrder</code> etc). </p>
  
  <p>Anyone have a clue what's going on? I'm lost here :(</p>
  
  <p><strong>Edit</strong>
  I've seen in other questions that people remove the TenantId from tables, this is not an option since primary keys are not unique across tenants and we want to keep the shared data architecture.</p>
  
  <p>I know the workaround is having a second TenantId in the SpecialOrder class, but this does not seem logical to me. </p>