1

Scenario I am playing MVC NerdDinner project and using ado.net entity data model instead on 'dbml'

I have 2 database tables Dinner & RSVP where RSVP contains DinnerID as foreign key from Dinner table.

Now when I am accessing a particular record from Dinner table, it returns a dinner object with RSVP property but there are no data with that property inspite of RSVP table having data with foreign key from Dinner table

Data

DinnerTable

ID :1
Title : '.Net Architecture'

RSVPTable

ID :1
Dinner ID: 1
AttendeeName : 'Miral'

ID :2
Dinner ID: 1
AttendeeName : 'Shivani'

So when fetching Dinner data which should return its child RSVP data, I am getting RSVP property with 0 records.

Jarrett Meyer
  • 19,333
  • 6
  • 58
  • 52
Miral
  • 5,968
  • 16
  • 57
  • 85

2 Answers2

2

EF is a little different from LINQ. In your query, add something like

var dinner = context.Dinners.First(d => d.DinnerID = id).Include("Attendees");

This will tell EF to attach the associated Attendees objects in a single fetch, doing all of the necessary joins for you.

Jarrett Meyer
  • 19,333
  • 6
  • 58
  • 52
  • Sorry but I am not able to find this particular method or extension method 'Include' – Miral Jun 09 '09 at 15:50
  • Over here its just related with Attendee name what if I require the whole objects with that particular dinner object. – Miral Jun 09 '09 at 15:52
  • http://msdn.microsoft.com/en-us/library/bb896272.aspx http://msmvps.com/blogs/matthieu/archive/2008/06/06/entityframework-include-with-func.aspx – Jarrett Meyer Jun 09 '09 at 16:34
  • 1
    Correct syntax var dinner =_nerdDinnerEntities.Dinner.Include("RSVP").Where(dd => dd.ID == ID).FirstOrDefault(); Your require to write Include before First i.e. 'Include' is a method on Entity over here 'Dinner'. Also it includes child table name i.e. 'RSVP' and the property 'AttendeeName'. I tried passing just the one of the property 'AttendeeName' but it didnt worked. – Miral Jun 10 '09 at 11:45
1

Correct syntax

Table : 'Dinner' & 'RSVP'

var dinner =_nerdDinnerEntities.Dinner.Include("RSVP").Where(dd => dd.ID == ID).FirstOrDefault();

Your require to write Include before FirstOrDefault.

'Include' is a method on Entity over here 'Dinner',and it includes table name containing the foreign key i.e. 'RSVP' and the property 'AttendeeName'.

I tried passing one of the property 'AttendeeName' but it didnt worked.

Miral
  • 5,968
  • 16
  • 57
  • 85