title: Null Reference Exception querying data in EF6 after upgrading from EF4
body: <p>I'm having some trouble getting data out of my entity framework context. I'm upgrading my application from entity framework 4 to 6 and it has not been a party. </p>
  
  <p>Right now, if I try to query any records from my database I get a null reference exception.</p>
  
  <pre><code>DBSet&lt;EntityObject&gt; test1 = context.EntityObjects;
  List&lt;EntityObject&gt; test2 = test1.ToList();
  </code></pre>
  
  <p>The first line runs without error. The second line throws a System.NullReferenceException with the following stack trace: </p>
  
  <pre><code>at System.Data.Entity.Core.Metadata.Edm.OSpaceTypeFactory.TypesMatchByConvention(Type type, EdmType cspaceType)
  at System.Data.Entity.Core.Metadata.Edm.OSpaceTypeFactory.TryCreateStructuralType(Type type, StructuralType cspaceType, EdmType&amp; newOSpaceType)
  at System.Data.Entity.Core.Metadata.Edm.OSpaceTypeFactory.TryCreateType(Type type, EdmType cspaceType)
  at System.Data.Entity.Core.Metadata.Edm.ObjectItemConventionAssemblyLoader.LoadTypesFromAssembly()
  at System.Data.Entity.Core.Metadata.Edm.ObjectItemAssemblyLoader.Load()
  at System.Data.Entity.Core.Metadata.Edm.AssemblyCache.LoadAssembly(Assembly assembly, Boolean loadReferencedAssemblies, ObjectItemLoadingSessionData loadingData)
  at System.Data.Entity.Core.Metadata.Edm.AssemblyCache.LoadAssembly(Assembly assembly, Boolean loadReferencedAssemblies, KnownAssembliesSet knownAssemblies, EdmItemCollection edmItemCollection, Action`1 logLoadMessage, Object&amp; loaderCookie, Dictionary`2&amp; typesInLoading, List`1&amp; errors)
  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.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.GetEnumerator()
  at System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable&lt;TResult&gt;.GetEnumerator()
  at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
  at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
  at ConnectServer.ConnectModule.handleReceive(Message message, ClientConnection client) in c:\Users\Eric Bowman\Documents\Visual Studio 2012\Projects\NXConnect_ME578_Interoperability\CADInteroperabilityCATIA\ConnectServer\ConnectModule.cs:line 137
  </code></pre>
  
  <p>My closes guess right now is that there's a problem with my connection string which is preventing the context from connecting with the database, so here is my config file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
     &lt;configuration&gt;
        &lt;configSections&gt;
           &lt;section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /&gt;
           &lt;!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --&gt;
        &lt;/configSections&gt;
        &lt;connectionStrings&gt;
           &lt;add name="FancyEntities" connectionString="metadata=res://*/ConnectData.csdl|res://*/ConnectData.ssdl|res://*/ConnectData.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;data source=localhost,1433;initial catalog=Fancy_Dev;Persist Security Info=True;User ID=******;Password=************;MultipleActiveResultSets=True&amp;quot;"
    providerName="System.Data.EntityClient"/&gt;
        &lt;/connectionStrings&gt;
        &lt;entityFramework&gt;
           &lt;defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /&gt;
           &lt;providers&gt;
              &lt;provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /&gt;
           &lt;/providers&gt;
        &lt;/entityFramework&gt;
        &lt;startup&gt;
           &lt;supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /&gt;
        &lt;/startup&gt;
  </code></pre>
  
  <p></p>
  
  <p>Am I missing something in there? Could it be something else?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>The connection string was bad, but even after fixing it I get exactly the same error. I can open a connection to the database, but when I try to look at any of the collections in the context, it throws a null reference exception. </p>
  
  <p>This is very frustrating</p>
  
  <p><strong>Another Update</strong></p>
  
  <p>I haven't seen much help so I'm posting some of the information I've found. </p>
  
  <p>I found the Entity Framework Source and this is the method that crashes: </p>
  
  <pre><code>    internal static bool TypesMatchByConvention(Type type, EdmType cspaceType)
      {
          return type.Name == cspaceType.Name;
      }
  </code></pre>
  
  <p>And here is the previous code block that calls the method: </p>
  
  <pre><code>        if (cspaceType.BaseType != null)
          {
              if (TypesMatchByConvention(type.BaseType, cspaceType.BaseType))
              {
                  TrackClosure(type.BaseType);
                  referenceResolutionListForCurrentType.Add(
                      () =&gt; ospaceType.BaseType = ResolveBaseType((StructuralType)cspaceType.BaseType, type));
              }
              else
              {
                  var message = Strings.Validator_OSpace_Convention_BaseTypeIncompatible(
                      type.BaseType.FullName, type.FullName, cspaceType.BaseType.FullName);
                  LogLoadMessage(message, cspaceType);
                  return false;
              }
          }
  </code></pre>
  
  <p>Note that it checks cspaceType.BaseType for null but not type.BaseType.</p>
  
  <p>I created a fresh project with the same entity model and it works fine, so it's something to do with my specific settings or project or something.</p>