title: Table-per-Hierarchy and Many to Many relationship
body: <p>this may seem very easy to solve, but the complication I'm having is that I have a table per hierarchy to store all of the entities and I'm not able to create the relationships over the same table. Here is what I have in the DB and classes:</p>
  
  <p><img src="https://i.stack.imgur.com/N75Gb.png" alt="enter image description here"></p>
  
  <p>I have just one table named BaseObject with an ID, name and type. I will create two classes for those entities stored in there. Master and Component. The type column is the discriminator. I have another table to store the relationships between both: A master can have many components and a component also can have many other components. </p>
  
  <p>This is the code I have for the classes:</p>
  
  <pre><code>public partial class BaseObject
  {
      public BaseObject()
      {
  
      }
  
      public System.Guid ID { get; set; }
      public string Name { get; set; }
      public Nullable&lt;int&gt; Type { get; set; }
  }
  
  public class MasterObject : BaseObject
  {
      public virtual ICollection&lt;ComponentObject&gt; Components { get; set; }
  
      public MasterObject()
      {
          this.Components = new List&lt;ComponentObject&gt;();
      }
  }
  
  public class ComponentObject : BaseObject
  {
      public virtual ICollection&lt;MasterObject&gt; MasterObjects { get; set; }
  
      public ComponentObject()
      {
          this.MasterObjects = new List&lt;MasterObject&gt;();
      }
  }
  </code></pre>
  
  <p>And these are the mappings:</p>
  
  <pre><code>public class BaseObjectMap : EntityTypeConfiguration&lt;BaseObject&gt;
  {
      public BaseObjectMap()
      {
          // Primary Key
          this.HasKey(t =&gt; t.ID);
  
          // Table &amp; Column Mappings
          this.ToTable("BaseObject");
          this.Property(t =&gt; t.ID).HasColumnName("ID");
          this.Property(t =&gt; t.Name).HasColumnName("Name");
  
          //configure the inheritance in here
          this.Map&lt;MasterObject&gt;(m =&gt; m.Requires("Type").HasValue(1));
          this.Map&lt;ComponentObject&gt;(m =&gt; m.Requires("Type").HasValue(2));
      }
  }
  
  public class MasterObjectMap : EntityTypeConfiguration&lt;MasterObject&gt;
  {
      public MasterObjectMap()
      {
          this.HasMany&lt;ComponentObject&gt;(t =&gt; t.Components)
              .WithMany(t =&gt; t.MasterObjects)
              .Map(c =&gt;
              {
                  c.ToTable("ObjectComponents");
                  c.MapLeftKey("ComponentObjectID");
                  c.MapRightKey("BaseObjectID");
              });
      }
  }
  
  public class ComponentObjectMap : EntityTypeConfiguration&lt;ComponentObject&gt;
  {
      public ComponentObjectMap()
      {
          this.HasMany&lt;MasterObject&gt;(t =&gt; t.MasterObjects)
              .WithMany(t =&gt; t.Components)
              .Map(m =&gt;
              {
                  m.ToTable("ObjectComponents");
                  m.MapLeftKey("BaseObjectID");
                  m.MapRightKey("ComponentObjectID");
              });
      }
  }
  </code></pre>
  
  <p>The thing is that when quering the DB, I can get a Master by accessing the DBSet Masters, but the Component collection always gives a non-sense exception saying that "Problem in mapping fragments starting at line 6:Condition member 'BaseObject.Type' with a condition other than 'IsNull=False' is mapped. Either remove the condition on BaseObject.Type or remove it from the mapping."</p>
  
  <p>I don't understand what's happening. Of course if the classes where pointing each to a table, this would be very easy, but I suspect that is the root of my problem. </p>
  
  <p>Also, I'm just starting with EF. I wanted to create the classes based on my existing DB that I wouldn't like to modify at all. Unless it's really needed. Please guide me if what I'm trying to do is right or wrong, or what should I do first to fully implement EF on this project that is currently using NHibernate.</p>
  
  <p>Any help here? Thanks</p>