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

时间:2014-08-04 21:18:24

标签: c# entity-framework

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

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

DBSet<EntityObject> test1 = context.EntityObjects;
List<EntityObject> test2 = test1.ToList();

第一行运行没有错误。第二行抛出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

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

<?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>

我错过了什么吗?可能是别的吗?

更新

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

这非常令人沮丧

另一次更新

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

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

    internal static bool TypesMatchByConvention(Type type, EdmType cspaceType)
    {
        return type.Name == cspaceType.Name;
    }

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

        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;
            }
        }

请注意,它会将cspaceType.BaseType检查为null,但不检查type.BaseType。

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

3 个答案:

答案 0 :(得分:5)

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

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

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

答案 1 :(得分:1)

我似乎无法在EF6文档中的任何位置找到EntityObjects属性。您确定从项目中完全删除了对EF4的所有引用吗?

也许你可以用这个代替你的代码:

EntityObject e = context.EntityObjects.FirstOrDefault();

看看e的价值是多少? 最后,如果您尝试从数据库中的表中选择所有对象并将它们放在List中,那么这个语句是不是一个选项?

var myObjects = (from s in context.tableName select s).ToList();

答案 2 :(得分:0)

我有一个类似的问题,每次我在DbSet EF上调用ToListAsync()时都会抛出NullReferenceException。对于那些最终来自谷歌的人来说(我无法在网上找到任何帮助),在我的情况下,同步调用异步代码是一种混淆(来自第三方框架的属性限制)。 / p>