title: Entity Framework Code First TPH inheritance - can different child classes share a field?
body: <p>I have an Entity Framework Model created using Entity Framework Code First that is using Table Per Hierarchy inheritance where the structure looks a little like this:</p>
  
  <pre><code>public abstract class BaseState 
  {
      public int Id { get; set; }
  
      public string StateName { get; set; }
  
      // etcetera
  }
  
  
  public class CreatedState : BaseState
  {
      public User Owner { get; set; }
  
  }
  
  public class UpdatedState : BaseState
  {
      public User Owner { get; set; }
  }
  </code></pre>
  
  <p>Now what that creates is in my <code>BaseStates</code> table I have <code>Owner_Id</code> and <code>Owner_Id1</code> stored. But given that no class will ever be both a <code>CreatedState</code> and an <code>UpdatedState</code> it seems as though it would be logical to use a single <code>Owner_Id</code> for both. Which would also make it easier to follow the database.</p>
  
  <p>My basic question is: Is this possible with Code First EF4? </p>
  
  <p>I have tried to map the columns:</p>
  
  <pre><code>public class CreatedState : BaseState
  {
      [Column("OwnerId")]
      public User Owner { get; set; }
  
  }
  
  public class UpdatedState : BaseState
  {
      [Column("OwnerId")]
      public User Owner { get; set; }
  }
  </code></pre>
  
  <p>That appeared to have no effect.</p>
  
  <p>Then I tried creating a shared parent class, which is probably more correct OO anyway:</p>
  
  <pre><code>public abstract class OwnedState : BaseState
  {
       public User Owner { get; set; }
  }
  
  
  public class CreatedState : OwnedState
  {
  
  }
  
  public class UpdatedState : OwnedState
  {
  
  }
  </code></pre>
  
  <p>Again, no dice. Or, more worryingly, this appears to work in some cases and not in others ( obviously my real configuration is slightly more complex ) when I can see precisely no difference between the classes where it does work. </p>
  
  <p>Edit for more detail on what fails:
  I have two fields that behave in the way I have described above, we might call the associated classes <code>OwnedState</code> and <code>ActivityState</code>, both of which I have created as an abstract class in the way shown in my last example. <code>OwnedState</code> has two classes that derive from it, <code>ActivityState</code> has three. In the database I have <code>ActivityState_Id</code> but also <code>OwnedState_Id</code> and <code>OwnedState_Id1</code>. </p>
  
  <p>I can see no difference at all between the <code>OwnedState</code> and <code>ActivityState</code> classes aside from the type that they reference ( both other entities ) and yet in the database it appears as though EF has somehow interpreted them differently- I don't understand the EF internals well enough to know how it makes that decision.</p>