title: EF6: Schema specified is not valid; ambiguous mapping
body: <p>I am using EF6 with the database first approach. The database has three simple tables:
  Person, Token and Template</p>
  
  <p>For the database I have generated the edmx model and entities</p>
  
  <p>Person.cs</p>
  
  <pre><code>public partial class Person
  {
      public Person()
      {
          this.Tokens = new HashSet&lt;Token&gt;();
      }
      public long Id { get; set; }
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public string PersonImage { get; set; }
      public string Repository { get; set; }
      public byte[] RepositoryHash { get; set; }
      public virtual ICollection&lt;Token&gt; Tokens { get; set; }
  }
  </code></pre>
  
  <p>Token.cs</p>
  
  <pre><code>public partial class Token
  {
      public long Id { get; set; }
      public string TokenImage { get; set; }
      public string NormalizedTokenImage { get; set; }
      public long PersonId { get; set; }
      public long TemplateId { get; set; }
      public string TokenType { get; set; }
      public string Repository { get; set; }
      public byte[] RepositoryHash { get; set; }
      public virtual Person Person { get; set; }
      public virtual Template Template { get; set; }
  }
  </code></pre>
  
  <p>Template.cs</p>
  
  <pre><code>public partial class Template
  {
      public Template()
      {
          this.Tokens = new HashSet&lt;Token&gt;();
      }
      public long Id { get; set; }
      public string TemplateFile { get; set; }
      public string TemplateType { get; set; }
      public string Repository { get; set; }
      public byte[] RepositoryHash { get; set; }
      public virtual ICollection&lt;Token&gt; Tokens { get; set; }
  }
  </code></pre>
  
  <p>The application is based on MEF and when needed it loads the <code>DataProviderModule</code> that provides data using EF6. The application workflow is divided in two phases. Phase one initializes required modules and loads config for these modules. Then I create some data and save it with <code>DataProviderModule</code> in database. Phase one ends here - so far so good.</p>
  
  <p>When the Phase Two starts, I clear the old modules references and load new set of modules with the new instance of <code>DataProviderModule</code>. But now, when I try to use EF inside the <code>DataProviderModule</code>, the MetadataException is thrown (when I try to get data from database)</p>
  
  <pre><code>using (var entities = new Entities(_connectionString))
  {
      return entities.People.Count() // &lt;-- MetadataException thrown here
  }
  </code></pre>
  
  <p>MetadataException</p>
  
  <pre class="lang-none prettyprint-override"><code>Schema specified is not valid. Errors: 
  </code></pre>
  
  <pre class="lang-none prettyprint-override"><code>The mapping of CLR type to EDM type is ambiguous because multiple CLR types match the EDM type 'Person'. 
  Previously found CLR type 'MSSQLDBLib.Model.Person', 
  newly found CLR type 'MSSQLDBLib.Model.Person'.
  
  The mapping of CLR type to EDM type is ambiguous because multiple CLR types match the EDM type 'Template'. 
  Previously found CLR type 'MSSQLDBLib.Model.Template', 
  newly found CLR type 'MSSQLDBLib.Model.Template'.
  
  The mapping of CLR type to EDM type is ambiguous because multiple CLR types match the EDM type 'Token'. 
  Previously found CLR type 'MSSQLDBLib.Model.Token', 
  newly found CLR type 'MSSQLDBLib.Model.Token'.
  </code></pre>
  
  <p>StackTrace</p>
  
  <pre class="lang-none prettyprint-override"><code>   at System.Data.Entity.Core.Metadata.Edm.ObjectItemCollection.LoadAssemblyFromCache(Assembly assembly, Boolean loadReferencedAssemblies, EdmItemCollection edmItemCollection, Action`1 logLoadMessage)
     at System.Data.Entity.Core.Metadata.Edm.ObjectItemCollection.ExplicitLoadFromAssembly(Assembly assembly, EdmItemCollection edmItemCollection, Action`1 logLoadMessage)
     at System.Data.Entity.Core.Metadata.Edm.MetadataWorkspace.ExplicitLoadFromAssembly(Assembly assembly, ObjectItemCollection collection, Action`1 logLoadMessage)
     at System.Data.Entity.Core.Metadata.Edm.MetadataWorkspace.LoadFromAssembly(Assembly assembly, Action`1 logLoadMessage)
     at System.Data.Entity.Core.Metadata.Edm.MetadataWorkspace.LoadFromAssembly(Assembly assembly)
     at System.Data.Entity.Core.Metadata.Edm.MetadataOptimization.TryUpdateEntitySetMappingsForType(Type entityType)
     at System.Data.Entity.Internal.InternalContext.TryUpdateEntitySetMappingsForType(Type entityType)
     at System.Data.Entity.Internal.InternalContext.UpdateEntitySetMappingsForType(Type entityType)
     at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
     at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
     at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
     at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
     at System.Linq.Queryable.Count[TSource](IQueryable`1 source, Expression`1 predicate)
     at MSSQLDBLib.MsSqlPersonalDataProvider.GetPersonsCount()
     at DirectoryCompareApp.ViewModel.MainViewModel.&lt;Start&gt;d__5f.MoveNext() in d:\pzajic\SecufaceCollection\SecufaceNG\Sources\SecufaceNG\DirectoryCompareApp\ViewModel\MainViewModel.cs:line 810
  </code></pre>
  
  <p>I've already searched, how to solve this problem and I only found that same class names within different namespaces may cause similar problem, but this is not my case because the classes are in the same namespace.</p>
  
  <p>Any advise will be greatly appreciated. Thank you.</p>