title: EF6 failing to build model for Table Split/Shared Primary Key + Base class?
body: <p><strong>The problem</strong></p>
  
  <p>I'm trying to share a large table (200+fields) around ~7 entities using table splitting as per <a href="https://stackoverflow.com/questions/20966328/ef6-table-splitting-vs-shared-primary-key-for-multiple-splits">my previous question</a>.</p>
  
  <p>EF6 requires navigation properties not just from the primary model to the child models, but between all the child models (which sucks).</p>
  
  <p><strong>Manual solution</strong></p>
  
  <p>This can be done manually:</p>
  
  <pre><code>public class Franchise
  {
      [Key]
      public int Id { get; set; }
      public virtual FranchiseEntity Entity { get; set; }
      public virtual FranchiseMiscellaneous Miscellaneous { get; set; }
  }
  
  [Table("Franchise")]
  public class FranchiseEntity
  {
      [Key]
      public int Id { get; set; }
      public virtual FranchiseEntity Entity { get; set; } // Ignored, but relevant when inheritance involved, below...
      public virtual FranchiseMiscellaneous Miscellaneous { get; set; }
  }
  
  [Table("Franchise")]
  public class FranchiseMiscellaneous
  {
      [Key]
      public int Id { get; set; }
      public virtual FranchiseEntity Entity { get; set;
      public virtual FranchiseMiscellaneous Miscellaneous { get; set; }  // Ignored, but relevant when inheritance involved, below...
  }
  </code></pre>
  
  <p>With fluent mappings:</p>
  
  <pre><code>public class FranchiseMapping : EntityTypeConfiguration&lt;Franchise&gt;
  {
      public FranchiseMapping()
      {
          HasRequired(x =&gt; x.Entity).WithRequiredPrincipal();
          HasRequired(x =&gt; x.Miscellaneous).WithRequiredPrincipal();
      }
  }
  
  public class FranchiseEntityMapping : EntityTypeConfiguration&lt;FranchiseEntity&gt;
  {
      public FranchiseEntityMapping()
      {
          Ignore(x =&gt; x.Entity);
          HasRequired(x =&gt; x.Miscellaneous).WithRequiredPrincipal(x =&gt; x.Entity);
      }
  }
  
  public class FranchiseMiscellaneousMapping : EntityTypeConfiguration&lt;FranchiseMiscellaneous&gt;
  {
      public FranchiseMiscellaneousMapping()
      {
          Ignore(x =&gt; x.Miscellaneous);
      }
  }
  </code></pre>
  
  <p>THIS WORKS. But it won't this will not scale up well with 7+ models.</p>
  
  <p><strong>Attempt to improve #1</strong></p>
  
  <p>I'd like to improve through Inheritance + DRY principle:</p>
  
  <pre><code>public abstract class SharedFranchiseIdBase
  {
      [Key]
      public int Id { get; set; }
      public virtual FranchiseEntity Entity { get; set; }
      public virtual FranchiseMiscellaneous Miscellaneous { get; set; }
  }
  
  public class Franchise : SharedFranchiseIdBase { ... }
  
  public class FranchiseEntity : SharedFranchiseIdBase { ... }
  
  public class FranchiseMiscellaneous : SharedFranchiseIdBase { ... }
  
  // Maybe generalize the mapping code too...
  </code></pre>
  
  <p>But this FAILS on first request with "Sequence contains more than one matching element":</p>
  
  <pre><code>System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---&gt; System.InvalidOperationException: Sequence contains more than one matching element
  Result StackTrace:  
  at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source, Func`2 predicate)
     at System.Data.Entity.ModelConfiguration.Configuration.Properties.Navigation.NavigationPropertyConfiguration.ConfigureDependentBehavior(AssociationType associationType, EdmModel model, EntityTypeConfiguration entityTypeConfiguration)
     at System.Data.Entity.ModelConfiguration.Configuration.Properties.Navigation.NavigationPropertyConfiguration.Configure(NavigationProperty navigationProperty, EdmModel model, EntityTypeConfiguration entityTypeConfiguration)
     at System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.ConfigureAssociations(EntityType entityType, EdmModel model)
     at System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.Configure(EntityType entityType, EdmModel model)
     at System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.ConfigureEntities(EdmModel model)
     at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
     at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
     at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
     at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
     at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
     at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
     at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
     at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
     at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
     at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source)
     ... // my test query function
  </code></pre>
  
  <p><strong>Attempt to improve #2</strong></p>
  
  <p>I thought I could declare them abstract, so at least the programmers are forced to implement the correct members (still sucks to re-declare on each derived class):</p>
  
  <pre><code>public abstract class SharedFranchiseIdBase
  {
      [Key]
      public int Id { get; set; }
      public abstract FranchiseEntity Entity { get; set; }
      public abstract FranchiseMiscellaneous Miscellaneous { get; set; }
  }
  
  public class Franchise : SharedFranchiseIdBase
  {
      [Key]
      public int Id { get; set; }
      public override FranchiseEntity Entity { get; set; }
      public override FranchiseMiscellaneous Miscellaneous { get; set; }
  }
  //etc for other classes
  </code></pre>
  
  <p>But this fails when the same error. Huh?? The class definitions are IDENTICAL as the working copy except that they are declared "override" instead of "virtual". It's as if E/F is indexing on PropertyInfos or something without regard for the PropertyInfo.ReflectedType</p>
  
  <p><strong>Attempt to improve #3</strong></p>
  
  <p>I could enforce the pattern using an interface, but this is less preferable as the interface has to be declared on each class which is starting to look pretty weird:</p>
  
  <pre><code>public class Franchise : SharedFranchiseIdBase, ISharedFranchiseId { ... }
  
  public class FranchiseEntity : SharedFranchiseIdBase, ISharedFranchiseId { ... }
  
  public class FranchiseMiscellaneous : SharedFranchiseIdBase, ISharedFranchiseId { ... }
  </code></pre>
  
  <p><strong>Huh?</strong></p>
  
  <p>Is this a bug in E/F, that it struggles to treat properties on the base class identical to those on the derived classes?</p>
  
  <p>Apologies for the long-winded explanation, it's the summary of this morning's entire investigation.</p>