How to Fix Missing records in Linq to Entities How to Fix Missing records in Linq to Entities

How to Fix Missing records in Linq to Entities

The Problem

You are attempting to use Linq to Entities in the standard way, your linq model does not seem to have any records attached.  You have missing records in Linq to Entities.

You look in the database, and the proper record is there.  You do a

context.MyEntry.Count().ToString()

and it tells you there is one record there, but still when you attempt to actually retrieve a record, for example like

context.MyEntry.First().FirstName

you get a white screen.  What gives?

The Cause

It turns out the offending code looks like this

namespace MyProject.Models
{
	public class MyEntry
	{
		[Key]
		public int MyEntryId { get; set; }
                public string EmailAddress { get; set; }
                public string LastName { get; set; }
                public string FirstName;
	}
}

The Solution

Did you notice what was missing?  You don’t have the needed

{ get; set; }

after FirstName, so Linq To Entities will not retrieve the record for the database.  It’s always hard to see something missing, it took me about 20 minutes and lots of checking the wrong places to see the omission with this problem.

 

Written By Steve French

 

Leave a Reply

Your email address will not be published. Required fields are marked *






Copyright 2011 Digital Tool Factory. All Rights Reserved. Powered by raw technical talent. And in this case, WordPress.