Entity Framework NullReferenceException calling ToList?

∥☆過路亽.° 提交于 2020-01-14 09:32:10

问题


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:


回答1:


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.



来源:https://stackoverflow.com/questions/4168136/entity-framework-nullreferenceexception-calling-tolist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!