title: EF 5.0 and object inheritance mapping to same table by combining contexts
body: <p>I have a problem concerning EF 5.0 and the mapping of inherited objects to the same tables as the super class.</p>
  
  <p>My project looks like this:</p>
  
  <pre><code>public class ContextA : DbContext
  {
      public DbSet&lt;EntityA&gt; EntitiesA { get; set; }
      public DbSet&lt;EntityB&gt; EntitiesB { get; set; }
  
      public ContextA(string connnectionString) : base(connnectionString) { }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          ModelCreating(modelBuilder);
      }
  
      public static void ModelCreating(DbModelBuilder modelBuilder)
      {
          modelBuilder.Entity&lt;EntityA&gt;().ToTable("EntityAs");
          modelBuilder.Entity&lt;EntityB&gt;().ToTable("EntityBs");
  
          modelBuilder.Entity&lt;EntityA&gt;().HasKey(e =&gt; e.EntityAId);
          modelBuilder.Entity&lt;EntityB&gt;().HasKey(e =&gt; e.EntityBId);
      }
  }
  
  public class EntityA
  {
      public int EntityAId { get; set; }
      public string Name { get; set; }
  }
  
  public class EntityB
  {
      public int EntityBId { get; set; }
      public string Name { get; set; }
  
      public int EntityAId { get; set; }
  }
  </code></pre>
  
  <p>So fine so good. Keep in mind, that the Entities A and B might be in a different schema of the database or could be in a different context which is the reason for the statc ModelCreation-method.</p>
  
  <p>Now I want to combine the contexts like this:</p>
  
  <pre><code>public class ContextB : DbContext
  {
      public DbSet&lt;EntityAA&gt; EntityAAs { get; set; }
      public DbSet&lt;EntityBB&gt; EntityBBs { get; set; }
  
      public ContextB(string connnectionString) : base(connnectionString) { }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          ModelCreating(modelBuilder);
      }
  
      public static void ModelCreating(DbModelBuilder modelBuilder)
      {
          ContextA.ModelCreating(modelBuilder);
  
          modelBuilder.Entity&lt;EntityAA&gt;().Map(m =&gt;
          {
              m.MapInheritedProperties();
              m.ToTable("EntityAs");
          }).HasKey(e =&gt; e.EntityAId)
          .Property(e =&gt; e.EntityAId)
          .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
  
          modelBuilder.Entity&lt;EntityBB&gt;().Map(m =&gt;
          {
              m.MapInheritedProperties();
              m.ToTable("EntityBs");
          })
          .HasKey(e =&gt; e.EntityBId)
          .Property(e =&gt; e.EntityBId)
          .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
  
          modelBuilder.Entity&lt;EntityAA&gt;()
                      .HasMany(ea =&gt; ea.EntityBs)
                      .WithRequired(eb =&gt; eb.EntityA)
                      .HasForeignKey(eb =&gt; eb.EntityAId);
      }
  }
  
  public class EntityAA : EntityA
  {
      private readonly List&lt;EntityBB&gt; _entityBbs = new List&lt;EntityBB&gt;();
  
      public virtual ICollection&lt;EntityBB&gt; EntityBs
      {
          get { return _entityBbs; }
      }
  }
  
  public class EntityBB : EntityB
  {
      public EntityAA EntityA { get; set; }
  }
  </code></pre>
  
  <p>I want to do this, because I want to have schema-specific contexts which I want to combine to a application-specific context in addition.</p>
  
  <p>The problem is now, that I get an error during model creation:</p>
  
  <pre><code>Ergebnis Meldung:   Die EfInheritance.Test.GlobalTests.InsertEntitiesContextB-Testmethode hat eine Ausnahme ausgelöst: System.InvalidOperationException: The foreign key component 'EntityAId' is not a declared property on type 'EntityBB'. Verify that it has not been explicitly excluded from the model and that it is a valid primitive property.
  Ergebnis StackTrace:    
  bei System.Data.Entity.ModelConfiguration.Configuration.Properties.Navigation.ForeignKeyConstraintConfiguration.Configure(EdmAssociationType associationType, EdmAssociationEnd dependentEnd, EntityTypeConfiguration entityTypeConfiguration)
     bei System.Data.Entity.ModelConfiguration.Configuration.Properties.Navigation.NavigationPropertyConfiguration.ConfigureConstraint(EdmAssociationType associationType, EdmAssociationEnd dependentEnd, EntityTypeConfiguration entityTypeConfiguration)
     bei System.Data.Entity.ModelConfiguration.Configuration.Properties.Navigation.NavigationPropertyConfiguration.ConfigureDependentBehavior(EdmAssociationType associationType, EdmModel model, EntityTypeConfiguration entityTypeConfiguration)
     bei System.Data.Entity.ModelConfiguration.Configuration.Properties.Navigation.NavigationPropertyConfiguration.Configure(EdmNavigationProperty navigationProperty, EdmModel model, EntityTypeConfiguration entityTypeConfiguration)
     bei System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.ConfigureAssociations(EdmEntityType entityType, EdmModel model)
     bei System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.Configure(EdmEntityType entityType, EdmModel model)
     bei System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.ConfigureEntities(EdmModel model)
     bei System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
     bei System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
     bei System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
     bei System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
     bei System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
     bei System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
     bei System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
     bei System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
     bei System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)
     bei System.Data.Entity.Internal.Linq.InternalSet`1.Add(Object entity)
     bei System.Data.Entity.DbSet`1.Add(TEntity entity)
     bei EfInheritance.Test.GlobalTests.InsertEntitiesContextB() in c:\temp\EfInheritance\EfInheritance.Test\GlobalTests.cs:Zeile 45.
  </code></pre>
  
  <p>In the end, my question is: </p>
  
  <ol>
  <li>Is it possible to work like this at all?</li>
  <li>If this is a possible solution, what could I do to get it to work?</li>
  </ol>
  
  <p>If I comment the ForeignKey-Statement in ModelCreation, it works fine except the result in the database after running a simple test:</p>
  
  <pre><code>using (var ctx = new ContextB("CodeFirstDatabase"))
          {
              EntityAA aa = new EntityAA();
              aa.Name = "Test_AA_01";
  
              EntityBB bb = new EntityBB();
              bb.Name = "Test_BB_01";
  
              aa.EntityBs.Add(bb);
  
              ctx.EntityAAs.Add(aa);
  
              ctx.SaveChanges();
          }
  </code></pre>
  
  <p>EntityBs
  EntityBId | Name       | EntityAId | Discriminator | EntityA_EntityAId
  1         | Test_BB_01 | 0         | EntityBB      | 1</p>
  
  <p>EntityAs
  EntityAId | Name       | Disciminator
  1         | Test_AA_01 | EntityAA</p>
  
  <p>Now there is an additional ForeignKeyColumn which I can't access as well as an additional discriminator-column.</p>
  
  <p>Does someone have an idea what to do, to get this working? I have the project ready to mail/attach it if necessary.</p>
  
  <p>Kind regards,</p>
  
  <p>Sven</p>