Bulb Flash: Foreign key objects from WCF service not propagated to Silverlight client!

Consider the case :- WCF uses LINQ to SQL to connect to a DB with extensive foreign key relationships. You set Serialization Mode= Unidirectional to ensure all the relationships and DB objects can be propagated to the client as datacontracts

Silverlight client connects to the WCF service.

In many of the cases(i could not come to a rule when) i noticed that though the foreign key was accessible as an object in the WCF service, the object was not propagated to the client.

Suppose there are 3 tables called City, Company and Person. Person works in a company(foreign key), company is in a city(foreign key).

So using LINQ to SQL you would expect a relationship like Company.City (you might need to do lazy loading by using the LoadWith method with the datacontext but you will get Person.Company.City on the server)

Sometimes i noticed that though Company.City was available on the server it was not available on the client. On the silverlight client you only got Company.CityID.

Infact when i checked the dbml, Company.City was not marked as a datamember(and hence is not propagated to the client). A few suggestions on the net promoted marking it as a datamember on the dbml but then one would have to take care every time the dbml was regenerated.

So for a more permanent solution

I created a Partial class  on the WCF service itself called Company and added a property which exposed the City Property. I marked this property as a Datamember.

public partial class Company
  {
      /// <summary>
      ///     LINQ to SQL does not pass the "City" object to Silverlight 
      ///     We need the City object for processing.
      /// </summary>
      [DataMember]
      public City CompanyCity
      {
          get { return City; }
          set
          { City = value; }
      }
  }

Now the Property CompanyCity was propagated to the silverlight client and i got my City object.

Hope this bulb flash saves you a couple of hours and a few grey cells!!!

 

Cennest!