从EF4升级后,在EF6中查询数据的空引用异常

Eri*_*ric 3 c# entity-framework

我在从实体框架上下文中获取数据时遇到了一些麻烦.我正在将我的应用程序从实体框架4升级到6并且它还没有成为派对.

现在,如果我尝试查询数据库中的任何记录,我会得到一个空引用异常.

DBSet<EntityObject> test1 = context.EntityObjects;
List<EntityObject> test2 = test1.ToList();
Run Code Online (Sandbox Code Playgroud)

第一行运行没有错误.第二行抛出System.NullReferenceException,其中包含以下堆栈跟踪:

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& 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& loaderCookie, Dictionary`2& typesInLoading, List`1& 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<TResult>.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
Run Code Online (Sandbox Code Playgroud)

我现在关闭的猜测是我的连接字符串存在问题,这阻止了上下文与数据库的连接,所以这是我的配置文件:

<?xml version="1.0" encoding="utf-8"?>
   <configuration>
      <configSections>
         <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
         <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
      </configSections>
      <connectionStrings>
         <add name="FancyEntities" connectionString="metadata=res://*/ConnectData.csdl|res://*/ConnectData.ssdl|res://*/ConnectData.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=localhost,1433;initial catalog=Fancy_Dev;Persist Security Info=True;User ID=******;Password=************;MultipleActiveResultSets=True&quot;"
  providerName="System.Data.EntityClient"/>
      </connectionStrings>
      <entityFramework>
         <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
         <providers>
            <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
         </providers>
      </entityFramework>
      <startup>
         <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
      </startup>
Run Code Online (Sandbox Code Playgroud)

我错过了那里的东西吗?它可能是别的吗?

UPDATE

连接字符串很糟糕,但即使在修复它之后我也会得到完全相同的错误.我可以打开与数据库的连接,但是当我尝试查看上下文中的任何集合时,它会抛出一个空引用异常.

这非常令人沮丧

另一个更新

我没有看到太多的帮助,所以我发布了一些我发现的信息.

我找到了Entity Framework Source,这是崩溃的方法:

    internal static bool TypesMatchByConvention(Type type, EdmType cspaceType)
    {
        return type.Name == cspaceType.Name;
    }
Run Code Online (Sandbox Code Playgroud)

以下是调用该方法的前一个代码块:

        if (cspaceType.BaseType != null)
        {
            if (TypesMatchByConvention(type.BaseType, cspaceType.BaseType))
            {
                TrackClosure(type.BaseType);
                referenceResolutionListForCurrentType.Add(
                    () => 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;
            }
        }
Run Code Online (Sandbox Code Playgroud)

请注意,它检查cspaceType.BaseType是否为null但不是type.BaseType.

我使用相同的实体模型创建了一个新项目,它工作正常,因此它与我的特定设置或项目或其他东西有关.

Eri*_*ric 5

当我发布这个时,我的连接字符串很糟糕,所以请注意,但实际上并不是问题所在

问题是名称冲突.我在Entity Framework中的一个类与我的其他一个引用中的类具有相同的名称.

因此,作为一般惯例,我建议为每个Entity Framework类提供一个前缀,以确保它永远不会与您在任何库中可能使用的任何类相同.