Adding a ToList() in the DinnerRepository.cs file solves this problem.
Here is what was happening.
In DinnersController.cs I have the code to display dinner details
// GET: /Dinners/Details/2
public ActionResult Details(int id)
Dinners dinner = dinnerRepository.GetDinner(id);
if (dinner == null)
When I set a breakpoint and I look at the dinner object, I see that the RSVPs collection has zero items. However, if I step into the GetDinner() method and I look at the entities to see if the RSVP objects are there, it says "Expanding the Results View
will enumerate the IEnumerable" If I don't expand them, then the dinner object that is returned has zero RSVPs in the collection. However, if I do expand the Results View, then the dinner object has a valid RSVPs collection!
CAN ANYONE EXPLAIN THIS?
However, I took advantage of this quirk (BUG?) and added a line in the GetDinner() method to examine the RSVPs collection before the dinner object is returned.
NOW IT WORKS EVERY TIME. HOWEVER IT SEEMS LIKE A VERY DUMB IDEA TO REQUIRE THE ToList() ENUMERATION IN ORDER TO GET THE RSVPs collection to be filled in.
Here is the code in DinnerRepository.cs:
public Dinners GetDinner(int id)
// NOTE: The RSVPs listing won't show up in the Dinners object unless it gets enumerated
var rList = entities.RSVPs.ToList();
return entities.Dinners.FirstOrDefault(d => d.DinnerID == id);
IS THIS A BUG IN THE VS2008 version of Entity Framework, that collections based on a foreign key relationship do not automatically get populated unless they are explicitly accessed to force the enumeration?
Is there a bug fix for this?