Matt Ruwe
Matt Ruwe

Reputation: 3406

Join two tables and return the entire result using EF 4 and RIA services

I'm building a Silverlight application using RIA services and the entity framework 4.0. I have two tables in my SQL database that I would like to join and return as a single result. First, my two tables:


Room:
id
RoomName
BuildingId

Building:
id
BuildingName

My Default domain service generated a default GetRooms method:

Public Function GetRooms() As IQueryable(Of Room)
    Return Me.ObjectContext.Rooms
End Function

But this doesn't appear to contain details about my buildings even though there is referencial integrity between Room.BuildingId and Building.Id. Can someone please point me in the right direction?

Upvotes: 0

Views: 2936

Answers (3)

Chris Anderson
Chris Anderson

Reputation: 8369

Depending upon your requirements, you might want to consider returning a collection of "presentation model" objects (with each object containing information about both the room and the building), rather than entities. You define a class in the server project, and populate a collection of objects of this type with the data from the entity model in a domain operation (which is passed to the client). More information on doing so is here: http://msdn.microsoft.com/en-us/library/ee707347(VS.91).aspx.

Upvotes: 1

Shawn Mclean
Shawn Mclean

Reputation: 57469

If you want the buildings to be included, you need to use the .Include("Building") to your code:

Public Function GetRooms() As IQueryable(Of Room)
    Return Me.ObjectContext.Rooms.Include("Building")
End Function

Upvotes: 1

thomasmartinsen
thomasmartinsen

Reputation: 1993

To optimice performance related tables are not included by default in a query. You need to mark the tables that you wish to include in your query with [Include]. Furthermore you need to include the table in your query. Tim Heuer have reading a blogpost about how to do it:

http://timheuer.com/blog/archive/2010/01/05/master-details-with-ria-services-and-includedresults.aspx

Upvotes: 1

Related Questions