[英]After upgrading to EF6 from EF5, my generated classes do not reference the new System.Data.Entity namespaces
[英]Null Reference Exception querying data in EF6 after upgrading from EF4
我在從實體框架上下文中獲取數據時遇到了一些麻煩。 我正在將我的應用程序從實體框架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="data source=localhost,1433;initial catalog=Fancy_Dev;Persist Security Info=True;User ID=******;Password=************;MultipleActiveResultSets=True""
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>
我錯過了那里的東西嗎? 它可能是別的嗎?
UPDATE
連接字符串很糟糕,但即使在修復它之后我也會得到完全相同的錯誤。 我可以打開與數據庫的連接,但是當我嘗試查看上下文中的任何集合時,它會拋出一個空引用異常。
這非常令人沮喪
另一個更新
我沒有看到太多的幫助,所以我發布了一些我發現的信息。
我找到了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。
我使用相同的實體模型創建了一個新項目,它工作正常,因此它與我的特定設置或項目或其他東西有關。
當我發布這個時,我的連接字符串很糟糕,所以請注意,但實際上並不是問題所在
問題是名稱沖突。 我在Entity Framework中的一個類與我的其他一個引用中的類具有相同的名稱。
因此,作為一般慣例,我建議為每個Entity Framework類提供一個前綴,以確保它永遠不會與您在任何庫中可能使用的任何類相同。
我似乎無法在EF6文檔中的任何位置找到EntityObjects屬性。 您確定從項目中完全刪除了對EF4的所有引用嗎?
也許你也可以用這個代替你的代碼:
EntityObject e = context.EntityObjects.FirstOrDefault();
看看e
的價值是什么? 最后,如果您嘗試從數據庫中的表中選擇所有對象並將它們放在List
,那么這個語句不是一個選項嗎?
var myObjects = (from s in context.tableName select s).ToList();
我有一個類似的問題,每次我在DbSet EF上調用ToListAsync()時都會拋出一個NullReferenceException。 對於那些從谷歌來到這里的人來說(我在網上找不到任何幫助),在我的情況下,它是同步調用異步代碼的混合(來自第三方框架的屬性限制)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.