title: How to create and use a generic class EntityTypeConfiguration<TEntity>
body: <h1>Code</h1>
  
  <p>I have two very simple interfaces in my application</p>
  
  <p><strong>Represents entities that are saved in the database</strong></p>
  
  <pre><code>public interface IEntity
  {
      int Id { get; set; }
  }
  </code></pre>
  
  <p><strong>Entities that have only the <code>Nome</code> field as required field to save the entity</strong></p>
  
  <pre><code>public interface IEntityName : IEntity
  {
      string Nome { get; set; }
  }   
  </code></pre>
  
  <p><strong>And many classes implementing this interface</strong></p>
  
  <pre><code>public class Modalidade : IEntityName
  {
      public int Id { get; set; }
      public string Nome { get; set; }
  }
  public class Nacionalidade : IEntityName
  {
      public int Id { get; set; }
      public string Nome { get; set; }
  }
  
  public class Profissao : IEntityName
  {
      public int Id { get; set; }
      public string Nome { get; set; }
  }
  
  public class TipoPessoa : IEntityName
  {
      public int Id { get; set; }
      public string Nome { get; set; }
  }
  </code></pre>
  
  <p><strong>Although they the same structure, are completely different fields used for instance in this class</strong></p>
  
  <pre><code>public class Pessoa : IEntity
  {
      public int Id { get; set; }
  
      public string CPF { get; set; }
      public string PIS { get; set; }
      public string RG { get; set; }
      public string OrgaoExpedidor { get; set; }
      public string TipoDocumento { get; set; }
      public DateTime? DataEmissao { get; set; }
  
      public virtual Nacionalidade Nacionalidade { get; set; }
      public virtual Profissao Profissao { get; set; }
  
      public virtual TipoPessoa Tipo { get; set; }
  
      ....
  }
  </code></pre>
  
  <h1>Problem</h1>
  
  <p>Order to facilitate (and not have to create a configuration class for each new class) created a generic class configuration:</p>
  
  <pre><code>internal class EntityNameConfiguracao&lt;TEntity&gt; : EntityTypeConfiguration&lt;TEntity&gt;
      where TEntity: class, IEntityName
  {
      public EntityNameConfiguracao()
      {
          Property(p =&gt; p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
          Property(p =&gt; p.Nome).IsRequired().HasMaxLength(255);
      }
  }
  </code></pre>
  
  <p><strong>This way, my setup, would</strong></p>
  
  <pre><code>modelBuilder.Configurations.Add(new EntityNameConfiguracao&lt;Modalidade&gt;());
  modelBuilder.Configurations.Add(new EntityNameConfiguracao&lt;TipoPessoa&gt;());
  modelBuilder.Configurations.Add(new EntityNameConfiguracao&lt;Nacionalidade&gt;());
  modelBuilder.Configurations.Add(new EntityNameConfiguracao&lt;Profissao&gt;());   
  </code></pre>
  
  <h1>Error</h1>
  
  <p>However, when trying to add a new migration, the following error occurs:</p>
  
  <p><strong>Command</strong>  </p>
  
  <p>Command in Package Manager Console: <code>Add-Migration PessoaDadosAdicionais</code>  </p>
  
  <p><strong>Error</strong>  </p>
  
  <pre><code>The property 'Id' is not a declared property on type 'Modalidade'. Verify that the property has not been explicitly excluded from the model by using the Ignore method or NotMappedAttribute data annotation. Make sure that it is a valid primitive property.
  </code></pre>