title: EF Fluent API: Set property for each entity derived from a base abstract class
body: <p>I have a BaseClass, which is abstract, and has many abstract properties.</p>
  
  <p>I have a dozen or so (it will probably grow) entities that are part of the Entity Framework that each derive from BaseClass.</p>
  
  <p>I'm trying to avoid having to do:</p>
  
  <pre><code>modelBuilder.Entity&lt;Entity1&gt;().HasKey(t =&gt; t.Id);
  modelBuilder.Entity&lt;Entity2&gt;().HasKey(t =&gt; t.Id);
  modelBuilder.Entity&lt;Entity3&gt;().HasKey(t =&gt; t.Id);
  ...
  </code></pre>
  
  <p>for each property and each entity, since that seems very wasteful and creates a lot of code duplication.  I experimented with getting all the Entities in a namespace that derive from the BaseClass by:</p>
  
  <pre><code>var derivedEntities = Assembly.GetExecutingAssembly().GetTypes().
                  Where(t =&gt; t.Namespace == "My.Entities" &amp;&amp; t.IsAssignableFrom(typeof(BaseClass)));
  </code></pre>
  
  <p>However, the next logical steps seems to be:</p>
  
  <pre><code>foreach (var entity in derivedEntities)
  {
      modelBuilder.Entity&lt;entity&gt;().HasKey(t =&gt; t.Id);
  }
  </code></pre>
  
  <p>but will not compile, because</p>
  
  <blockquote>
    <p>"entity is a variable, but is used like a type".</p>
  </blockquote>