title: Entity Framework Code First Base Class Not Mapped To DB
body: <p>I am using the Entity Framework Code First in C# and I have many entities that have the same columns used for tracking. The columns I am using are Active, IsDeleted, CreatedBy, ModifiedBy, DateCreated, and DateUpdated. It seems tedious to have to add these properties to every entity that I want to track. I would like to have a base class that my entities can inherit from such as the one below.</p>
  
  <pre><code>public abstract class TrackableEntity
  {
      public bool Active { get; set; }
      public bool IsDeleted { get; set; }
      public virtual User CreatedBy { get; set; }
      public virtual User ModifiedBy { get; set; }
      public DateTime DateCreated { get; set; }
      public DateTime DateModified { get; set; }
  }
  </code></pre>
  
  <p>Then I could inherit from this class in my entities to have these properties and when the database gets generated it would have these columns for each entity.</p>
  
  <pre><code>public class UserProfile : TrackableEntity, IValidatableObject
  {
      public int Id { get; set; }
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public string Email { get; set; }
      public string Phone { get; set; }
  
      public bool IsValid { get { return this.Validate(null).Count() == 0; } }
  
      public IEnumerable&lt;ValidationResult&gt; Validate(ValidationContext validationContext)
      {
          if (String.IsNullOrEmpty(FirstName))
              yield return new ValidationResult("First name cannot be blank", new[] { "Username" });
  
          if (String.IsNullOrEmpty(LastName))
              yield return new ValidationResult("Last cannot be blank", new[] { "Password" });
  
          //Finish Validation Rules
      }
  }
  </code></pre>
  
  <p>I really want to cut back on code duplication and use some type of method like this but I can't get this to work. I keep receiving the error below:</p>
  
  <blockquote>
    <p>Unable to determine the principal end of an association between the types 'Namespace.Models.User' and 'Namespace.Models.User'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.</p>
  </blockquote>
  
  <p>I have been searching around for a few days now and can't find an answer for what I am wanting to do. I really don't want to create a separate table that everything links to. I have read about TPT, TPH, and TPC. TPC seemed pretty close to what I want but sharing PKs across pretty much all my tables is some I definitely don't want to do.</p>
  
  <p>Here is an example of what I would like my tables to look like. These tables would be created by entities that inherit from the TrackableEntity.</p>
  
  <pre><code>[UserProfile]
      [Id] [int] IDENTITY(1,1) NOT NULL
      [FirstName] [varchar](50) NOT NULL
      [LastName] [varchar](50) NOT NULL
      [Email] [varchar](255) NOT NULL
      [Phone] [varchar](20) NOT NULL
      [CreatedBy] [int] NOT NULL
      [ModifiedBy] [int] NOT NULL
      [DateCreated] [datetime] NOT NULL
      [DateModified] [datetime] NOT NULL
      [Active] [bit] NOT NULL
      [IsDeleted] [bit] NOT NULL
  
  [CaseType]
      [Id] [int] IDENTITY(1,1) NOT NULL
      [Name] [varchar](50) NOT NULL
      [CreatedBy] [int] NOT NULL
      [ModifiedBy] [int] NOT NULL
      [DateCreated] [datetime] NOT NULL
      [DateModified] [datetime] NOT NULL
      [Active] [bit] NOT NULL
      [IsDeleted] [bit] NOT NULL
  </code></pre>