title: Restricting which classes are considered as model classes in EF
body: <p>I have a large number of classes, some of which are model classes and some of which are not. I'm trying to get only the model classes to work with a DB using EF6. Getting over the minor inconvenience that I can't have both <code>ModelNamespace1.ClassA</code> and <code>ModelNamespace2.ClassB</code> (I got over this by using Fluent API's <code>Ignore&lt;&gt;()</code> method on the <code>ModelBuilder</code> instance), I'm now facing the problem that classes which are not supposed to be part of the model classes are considered as such. </p>
  
  <p>As a result of this, after my custom code in <code>OnModelCreating()</code> finishes executing, I get this exception for many classes which are not even part of my model (conceptually):</p>
  
  <p><code>System.Data.Entity.ModelConfiguration.ModelValidationException: One or more validation errors were detected during model generation:
  Translation.Tester.RuleResult: : EntityType 'RuleResult' has no key defined. Define the key for this EntityType.</code></p>
  
  <p>Why is this happening, what am I doing wrong and is there a way to restrict EF to only considering classes in a list of namespaces (e.g. <code>ModelNamespace1.*</code>, <code>ModelNamespace2.*</code> but not <code>NonModelNamespace.*</code>)? </p>
  
  <p>My guess is that it's looking at every class that's being referenced from my model classes assemblies and considering that for mapping as well. </p>
  
  <p><strong>Later edit:</strong></p>
  
  <p>Having consulted <a href="https://github.com/mono/entityframework/blob/master/src/EntityFramework/ModelConfiguration/Mappers/TypeMapper.cs#L23" rel="nofollow">EF code</a> It seems that after <code>OnModelCreating()</code> is called, when types are actually mapped, an instance of the <code>TypeMapper</code> class is created internally, the constructor of which looks like this:</p>
  
  <pre><code>public TypeMapper(MappingContext mappingContext)
  {
      DebugCheck.NotNull(mappingContext);
  
      _mappingContext = mappingContext;
  
      _knownTypes.AddRange(
          mappingContext.ModelConfiguration
                        .ConfiguredTypes
                        .Select(t =&gt; t.Assembly)
                        .Distinct()
                        .SelectMany(a =&gt; a.GetAccessibleTypes().Where(type =&gt; type.IsValidStructuralType())));
  }
  </code></pre>
  
  <p>This code leads to loading all accessible types from the assemblies where model classes are defined. This results in having known types which are not part of the model. </p>