title: Generic classes in EF Code First
body: <p>I’m trying to incorporate EF in my application. I use EF6 and Code First. My object model has some generic classes and it seems that Code First accepts it partially (tables and columns are generated correctly).</p>
  
  <p>Below an example:</p>
  
  <pre><code>public class Group : TimeableObject&lt;GroupTimeline&gt; {}
  
  public class GroupTimeline : TimelineBase
  {
      public GroupTimeline() { }
      public string Name {get; set;}
  }
  
  public abstract class TimelineBase
  {
      public int Id { get; set; }
      public DateTime BeginDate { get; set; }
      public DateTime EndDate { get; set; }
  }
  
  public abstract class TimeableObject&lt;T&gt;
      where T : TimelineBase
  {
      private DateTime? _snapshotDate;
  
      public TimeableObject()
      {
          Timelines = new List&lt;T&gt;();
      }
  
      public int Id { get; set; }
      public DateTime BeginDate { get; set; }
      public DateTime EndDate { get; set; }
  
      public List&lt;T&gt; Timelines { get; set; }
  
      public DateTime SnapshotDate
      {
          get { //some logic.. }
          set { _snapshotDate = value; }
      }
  
      public T Timeline
      {
          get { //some logic.. }
      }
  }
  </code></pre>
  
  <p>Through fluent API I configured the inheritance as TPC. So I expect a table Groups and a table GroupTimeline with a foreign key to Groups in GroupTimeline.</p>
  
  <p>First of all, the generated database looks correct except for the identity. The strange thing about this is that EF sees the Id property in e.g. Group and generates a PK in the database. Althought without an identity. The same for the Id property in the TimelineBase class.
  Second, I don't want the property SnapshotDate to be mapped. Through fluent API I was not able to reach this. Here I get an exception telling me that the complete class is ignored or is a generic. So I assume that EF does not support generic classes although the database is generated correctly (not the identity). What's the story behind this??
  With the use of data annotations I fixed my issues. I can get an identity with the attributes (on the Id property):</p>
  
  <pre><code>[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
  </code></pre>
  
  <p>and I can prevent mapping the SnapshotDate property with:</p>
  
  <pre><code>[NotMapped]
  </code></pre>
  
  <p>below my fluent configuration:</p>
  
  <pre><code>    protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          modelBuilder.Entity&lt;Group&gt;().Map(m =&gt;
          {
              m.MapInheritedProperties();
          });
          modelBuilder.Entity&lt;GroupTimeline&gt;().Map(m =&gt;
          {
              m.MapInheritedProperties();
              m.ToTable("Group_Timeline");
          });
  
        modelBuilder.Entity&lt;TimeableObject&lt;TimelineBase&gt;&gt;().HasKey(m =&gt; m.Id).Property( p =&gt; p.Id ).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        modelBuilder.Entity&lt;TimeableObject&lt;TimelineBase&gt;&gt;().Ignore(m =&gt; m.SnapshotDate);
        modelBuilder.Entity&lt;TimelineBase&gt;().HasKey(m =&gt; m.Id).Property(p =&gt; p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
      }
  </code></pre>
  
  <p>This throw's an exception:</p>
  
  <pre><code>The type 'Data.DataObjectModel.TimeableObject`1[Data.DataObjectModel.TimelineBase]' 
  was not mapped. Check that the type has not been explicitly excluded by using the 
  Ignore method or NotMappedAttribute data annotation. Verify that the type was 
  defined as a class, is not primitive or generic, and does not inherit from 
  EntityObject.
  </code></pre>
  
  <p>Regards,
  Daniel Kaya</p>