Entity framework 4 实体框架NullReferenceException调用ToList?

Entity framework 4 实体框架NullReferenceException调用ToList?,entity-framework-4,firebird,firebird2.5,Entity Framework 4,Firebird,Firebird2.5,我对WPF和EF非常陌生,我正在尝试在datagrid中显示表中的一些数据。我已经从一个现有的数据库中提取了实体模型,并且简单的操作似乎很有效(使用“first”获取行数) 我正在使用2.0.5 DDEX提供程序和2.5.2 ADO NETProvider运行Firebird 2.5.0 当我试图将数据放入网格或简单地放入列表时,我会得到一个空引用异常 可能我只是不明白如何使用实体框架,但是我在网上看到的例子让它看起来非常简单 public partial class Page1 : Page

我对WPF和EF非常陌生,我正在尝试在datagrid中显示表中的一些数据。我已经从一个现有的数据库中提取了实体模型,并且简单的操作似乎很有效(使用“first”获取行数)

我正在使用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;
    }
}

InnerException:

我的表有一个多字段主键,其中一些字段可以为空。实体框架不喜欢主键中的可空字段。我删除了那些行,效果很好。我已经在寻找一个不同的解决方案,以满足促使我们在一些主键字段中允许空值的要求。

您可以发布NRE的完整堆栈跟踪吗?您是否使用日期为2010年2月17日的提供程序?如果是这样的话,我猜它不支持EF4,因为2010年8月16日的开发者报告中有这样一句话:“对实体框架v4的支持”。是的,我使用的是2010年2月17日的提供者。我将考虑在没有EF的情况下执行此操作(这是一个只有几个表的小项目)。我仍然希望您能提供有关此特定错误源的任何信息。当我发布此信息时,我仔细查看了异常,但无法找出是什么null对象导致了此问题。上面的堆栈跟踪请求促使我仔细查看堆栈跟踪,这是我以前没有看到的。我看到有'EntityKey'和'AddHashValue',我突然想到我的密钥中有一些空值(我正在努力纠正的一个根本错误的设计决策),这可能是问题的原因。tl;dr:我删除了主键字段中有空值的行,它工作正常。这也适用于视图。我将列从不可为null更改为可为null。然后我犯了这个错误。
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)