Entity Framework NullReferenceException calling ToList?
Asked Answered
T

2

8

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:

Thapsus answered 12/11, 2010 at 19:4 Comment(4)
Can you post the full stack trace of the NRE?Compulsory
Are you using the provider dated 2-17-2010? If so, I'm guessing it doesn't support EF 4 since the developer report from 8-16-2010 includes this note: "Work on Entity Framework v4 support".Ernst
Yes, I'm using the 2-17-2010 provider. I will see about doing this without the EF (it's a small project with only a few tables). I would still appreciate any input on the source of this particular error.Thapsus
When I posted this, I poked around in the exception, but couldn't figure out what null object was causing the problem. The stack trace request above prompted me to look closer at the stack trace, which I didn't see before. I see there 'EntityKey' and 'AddHashValue' and it occurs to me that I have some null values in my key (a fundamentally misguided design decision I'm working to rectify) and that this may be the cause of the problem. tl;dr: I removed the rows that have nulls in their primary key fields, and it works fine.Thapsus
T
12

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.

Thapsus answered 15/11, 2010 at 21:16 Comment(1)
This also applies to views. I changed a column from non nullable to nullable. Then I had this error.Hanhhank
C
1

I have been looking a reason to understand why it happens and how to fix it, but no solution or advice works for me. However I got a simple way to extract the information from my database.

STEP 1
First, you need to extract the SQL statement that is associated to your database using the following code line:

String querySQL = db.SALESREP.Sql;

STEP 2
Then, you just need to execute that statement with the next method:
var list = db.Database.SqlQuery<SALESREP>(querySQL).ToList();

This has been working fine for me, I hope it works for you too.
Cogwheel answered 26/7, 2023 at 16:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.