1

I have a simple Linq query that is failing suddenly. This worked up until yesterday and I have not been able to determine the problem.

The query is simply getting a complete view from SQL Server

using(JobEntities JE = new JobEntities())
{
     var BatchData = (from x in JE.vwBatchObjectActiveBatches select x).ToList();
}

Starting yesterday this line gets a

NullReferenceException (Object reference not set to an instance of an object)

My suspicion was that a user put in bad data causing the view to fail on SQL Server, but I have checked SQL Server itself and the view runs fine and populates with data.

This query was running in the middle of a larger function loading data from many places, so I have created a test case where I simply load the main window and run this query directly in the code behind to make sure that nothing else is affecting it, and the query still fails. All other Linq queries that I run in this project work still, only this one fails. The app is not under any production right now, and has been static for several months at least.

When I look at the JE in the watch window I can see the vwBatchObjectActiveBatches and it lists 164 records in the Local section -- this matches the view results on SQL Server. Expanding the Results View shows the null error again.

DebugInformation

How can I find and fix whatever is causing this query to fail? Why does the results view show an error but the local Line shows the data that I am attempting to get?

4
  • What is the type of vwBatchObjectActiveBatches? Please refer to Minimal, Complete, and Verifiable example. Commented Oct 26, 2016 at 12:44
  • can you please post the TargetSite message? Seems to be something with your entity key (simple guess) Commented Oct 26, 2016 at 12:45
  • (from x in JE.vwBatchObjectActiveBatches select x).ToList() === JE.vwBatchObjectActiveBatches.ToList() minus some contortions! Commented Oct 26, 2016 at 12:46
  • Probably caused by a nullable column. Make entity property nullable too. Commented Oct 26, 2016 at 12:52

1 Answer 1

2

It looks like your database returns NULL where Entity Framework does not expect/allow it. Data returned should be in accordance with the definition of its datamodel objects.

Solution: either 'fix' the data, or fix the query that produces it, or change the definition of your datamodel objects to allow NULL for the conflicting field(s).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.