框架NullReferenceException调用ToList

框架NullReferenceException调用ToList

本文介绍了实体框架NullReferenceException调用ToList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常喜欢WPF和EF,而我正在尝试从datagrid中的表中显示一些数据。我已经从现有的数据库中获取了实体模型,并且简单的操作似乎正常工作(使用第一获取行数)。



我正在使用Firebird 2.5.0使用2.0.5 DDEX提供程序和2.5.2 ADO NETProvider。



当我尝试将数据导入网格或简单地列入列表中时,我得到一个空引用异常。



可能我只是不明白如何使用实体框架,但是我在网上看到的例子看起来真的很简单。 p>

  public partial class Page1:Page 
{
实体上下文;

public Page1()
{
context = new Entities();

InitializeComponent();

//这个工作可以在网格中获取一行
var arep = context.SALESREPs.First();
var alist = new List< SALESREP>();
alist.Add(arep);
gridUserList.ItemsSource = alist;

//这两个都通过null引用异常失败
var allreps = context.SALESREPs.ToList();
gridUserList.ItemsSource = context.SALESREPs;
}
}

以下是异常详细信息:

  System.NullReferenceException被用户代码未处理
Message = Object引用未设置为对象的实例。
Source = System.Data.Entity
StackTrace:
在System.Data.EntityKey.AddHashValue(Int32 hashCode,Object keyValue)
在System.Data.EntityKey.GetHashCode()
在System.Collections.Generic.GenericEqualityComparer`1.GetHashCode(T obj)
在System.Collections.Generic.Dictionary`2.FindEntry(TKey键)
在System.Collections.Generic .Dictionary`2.TryGetValue(TKey key,TValue& value)
在System.Data.Objects.ObjectStateManager.TryGetEntityEntry(EntityKey key,EntityEntry& entry)
在System.Data.Common.Internal.Materialization .Shaper.HandleEntityAppendOnly [TEntity](Func`2 constructEntityDelegate,EntityKey entityKey,EntitySet entitySet)
在lambda_method(Closure,Shaper)
在System.Data.Common.Internal.Materialization.Coordinator`1.ReadNextElement (整形器)
在System.Data.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext()
在System.Collections.Generic.List`1..ctor(IEnumerable`1 coll在C:\Documents和Settings\d中的PSUserMaintenanceWebUI.Page1..ctor()中的System.Linq.Enumerable.ToList [TSource](IEnumerable`1源)

... \my documents\visual studio 2010 \Projects\UserMaintenance\UserMaintenanceWebUI\Page1.xaml.cs:line 36
at System.Xaml.Schema.XamlTypeInvoker.DefaultCtorXamlActivator.InvokeDelegate(Action`1 action,对象参数)
在System.Xaml.Schema.XamlTypeInvoker.DefaultCtorXamlActivator.CallCtorDelegate(XamlTypeInvoker类型)
在System.Xaml.Schema.XamlTypeInvoker.DefaultCtorXamlActivator.CreateInstance(XamlTypeInvoker类型)
在系统。 Xaml.Schema.XamlTypeInvoker.CreateInstance(Object [] arguments)
在MS.Internal.Xaml.Runtime.ClrObjectRuntime.CreateInstanceWithCtor(XamlType xamlType,Object [] args)
在MS.Internal.Xaml.Runtime .ClrObjectRuntime.CreateInstance(XamlType xamlType,Object [] args)

InnerException:

解决方案

我的表有一个多字段主键,其中一些字段为空。实体框架不喜欢主键中的可空字段。我删除了这些行,它工作正常。我已经在寻找一个不同的解决方案,促使我们在一些主要关键字段中允许使用null。


I'm very new to WPF and the EF, and I'm trying to display some data from a table in a datagrid. I've got the entity model pulled from an existing database and simple operations seem to work (getting row counts, using 'first').

I'm running against Firebird 2.5.0 using the 2.0.5 DDEX provider and 2.5.2 ADO NETProvider.

When I try to get the data into the grid or simply into a list, I get a null reference exception.

Possibly I just don't understand how to use the entity framework, but the examples I see on the net make it look really easy.

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

Here's the exception detail:

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:

解决方案

My table has a multi-field primary key with some of the fields being nullable. The entity framework doesn't like nullable fields in the primary key. I removed those rows and it works fine. I'm already in the process of finding a different solution to the requirement that prompted us to allow nulls in some of the primary key fields.

这篇关于实体框架NullReferenceException调用ToList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 14:29