Scott
Scott

Reputation: 904

Silverlight - how to return associated entities back to the client

Ok, I’ve spent two days on this and am losing confidence in both myself and the Entity Framework. I posted a question similar to this yesterday, but since I did not phrase it right, I was unable to get my issue resolved completely. Here it goes again.

First of all, I am writing a Silverlight application using RIA Services with the Entity Framework. In my database I have two very simple tables: HeaderTable and DetailsTable. The relationship between the two is a 1 to many. The EDM generated properties to navigate back and forth between these two entities. For example, in HeaderTable, I have a property called DetailTables that is a collection of all the DetailTalbe entities for the current HeaderTable entity.

Keeping it simple, from the client I want to return ALL of the HeaderTable entities. I do it like this:

public void TestFromClient()
{
  if (context == null)
  {        
    context = new TestContext();
    EntityQuery<HeaderTable> query = context.GetHeaderTablesQuery();
    context.Load<HeaderTable>(query);
  }

Within my callback method I do in fact get a collection to all the HeaderTable items. However, the property (DetialTables) which should hold all of my detail records is empty. Since these entities are related, I figured I would get them during the query. So, once I found I was not getting these entities, I made changes to my GetHeaderTables() on the server as follows:

public IQueryable<HeaderTable> GetHeaderTables()
{
  //return this.ObjectContext.HeaderTable;  // Original
  return this.ObjectContext.HeaderTable.Include("DetailTables");
}

This should now be explicitly bringing back my details within my headers, but just like my first attempt, by the time it gets to the client, the DetailTalbles property within my HeaderTable property is empty. Just as a test, I decided to see what was going on within the server function GetHeaderTables() and did this so I could debug the value:

public IQueryable<HeaderTable> GetHeaderTables()
{
  //return this.ObjectContext.HeaderTable;  // Original
  //return this.ObjectContext.HeaderTable.Include("DetailTables");
  List<HeaderTable> test = this.ObjectContext.HeaderTable.Include("DetailTables").ToList();
}

Sure enough, all of my HeaderTable entities had a valid DetailTables property with a collection of all the details. So, it is working on the server, but it is not working on the client. I am clearly missing something, but I can’t figure out what. If anyone can see what I am doing wrong or have advice for going at this a different way, I’m all ears.

As a side note, I also can’t figure out why I am not able to specify the Include() on the client side context. Why only the server? Man, I’m lost!

-Scott

Upvotes: 0

Views: 402

Answers (1)

Stephan
Stephan

Reputation: 5488

You need to have the [Include] attribute on your entity on the server side context.

[Include]
public EntitySet<DetailTable> DetailTables { get; set; }

This should be included in your metadata files for the model on the server.

Upvotes: 1

Related Questions