3
我對WPF和EF非常陌生,我試圖從數據網格中的表中顯示一些數據。我已經從現有的數據庫中提取實體模型,並且簡單的操作似乎可行(獲得行計數,使用'first')。實體框架NullReferenceException調用ToList?
我使用2.0.5 DDEX提供程序和2.5.2 ADO NETProvider針對Firebird 2.5.0運行。
當我嘗試將數據放入網格或簡單地放入列表中時,我得到一個空引用異常。
可能我只是不明白如何使用實體框架,但我在網上看到的例子使它看起來很容易。
public partial class Page1 : Page
{
Entities context;
public Page1()
{
context = new Entities();
InitializeComponent();
// This works to get a row into the grid
var arep = context.SALESREPs.First();
var alist = new List<SALESREP>();
alist.Add(arep);
gridUserList.ItemsSource = alist;
// These both fail with null ref exception
var allreps = context.SALESREPs.ToList();
gridUserList.ItemsSource = context.SALESREPs;
}
}
下面是異常詳細信息:
System.NullReferenceException was unhandled by user code
Message=Object reference not set to an instance of an object.
Source=System.Data.Entity
StackTrace:
at System.Data.EntityKey.AddHashValue(Int32 hashCode, Object keyValue)
at System.Data.EntityKey.GetHashCode()
at System.Collections.Generic.GenericEqualityComparer`1.GetHashCode(T obj)
at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
at System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)
at System.Data.Objects.ObjectStateManager.TryGetEntityEntry(EntityKey key, EntityEntry& entry)
at System.Data.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet)
at lambda_method(Closure , Shaper)
at System.Data.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)
at System.Data.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at PSUserMaintenanceWebUI.Page1..ctor() in C:\Documents and Settings\d...\my documents\visual studio 2010\Projects\UserMaintenance\UserMaintenanceWebUI\Page1.xaml.cs:line 36
at System.Xaml.Schema.XamlTypeInvoker.DefaultCtorXamlActivator.InvokeDelegate(Action`1 action, Object argument)
at System.Xaml.Schema.XamlTypeInvoker.DefaultCtorXamlActivator.CallCtorDelegate(XamlTypeInvoker type)
at System.Xaml.Schema.XamlTypeInvoker.DefaultCtorXamlActivator.CreateInstance(XamlTypeInvoker type)
at System.Xaml.Schema.XamlTypeInvoker.CreateInstance(Object[] arguments)
at MS.Internal.Xaml.Runtime.ClrObjectRuntime.CreateInstanceWithCtor(XamlType xamlType, Object[] args)
at MS.Internal.Xaml.Runtime.ClrObjectRuntime.CreateInstance(XamlType xamlType, Object[] args)
的InnerException:
你可以發佈NRE的完整堆棧跟蹤嗎? – 2010-11-12 19:11:03
您使用2010年2月17日提供的提供程序嗎?如果是這樣,我猜測它不支持EF 4,因爲2010年8月16日的開發者報告包含這個註釋:「在實體框架v4支持上工作」。 – 2010-11-12 19:35:21
是的,我正在使用2-17-2010提供程序。我會在沒有EF的情況下做這件事(這是一個只有幾張桌子的小項目)。對於這個特定錯誤的來源,我仍然會很感激。 – DaveK 2010-11-15 16:32:54