VS2008 the RSVP sets are not showing up in the Dinners object

Nov 2, 2010 at 1:53 PM

The links to the RSVP sets are not showing up in the Dinners object. I followed the instructions to use the ADO.Net Entity Data Model. But of course, in VS2008 the Entity Framework does not automatically implement the foreign key relationship. When I downloaded the VS 2008 solution I see that it does not use the Entity Framework, but uses Link to SQL instead.

a. Is the reason that the VS 2008 solution uses Link to SQL Classes (.dbml) instead of Entities (.edmx), because it doesn't implement the foreign key relationships?

b. Is it possible to get the RSVP sets to work using the Entity Framework, and if so, how would the code have to be written ? 

Thanks,

Robert Tanenbaum

Nov 3, 2010 at 5:13 PM

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        

[HttpGet]        

public ActionResult Details(int id)        

{            

Dinners dinner = dinnerRepository.GetDinner(id);            

if (dinner == null)                

return View("NotFound");            

else                

return View(dinner);        

}

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?