Binding parent and child data into DataGrid in ria services for silverlight 4
i've started learning Silverlight 4 RIA services. i've gone over alot of samples of how to bind data to a grid. but always there object being bound is a simple one with no child tables.
in my DB there's a table for employees and a table for city names (with id field as pk). in the employee table the开发者_如何学Gores a FK to the CityId field.
the first thing i've tried to do was to show a list of employees and their city name. this i've done in the normal way shown in all the tutorials (create edmx, create domain service and using the datasource window to create the datagrid)
the problems started when i tried binding the name of the city throw the FK between employee (parent entity) and citytypes (child entity)
this line works fine:
<sdk:DataGridTextColumn x:Name="cityCodeColumn" Binding="{Binding Path=CityCode}"
Header="CityCode" Width="SizeToHeader" />
this line does not:
<sdk:DataGridTextColumn x:Name="cityNameColumn" Binding="{Binding Path=CityType.Name}" Header="CityName" Width="SizeToHeader" />
after reading some more i've realized that the the domain service does not pass only the data of the entity selected by the LINQ command, and does not pass child entities info. unless using the include attribute.
so my question is , is there a pattern of building a silverlight application with out signing all the associations between entities as included?
thanks, Oren
To have the City information available when binding your Employee record you need to ensure you are marking the City reference with an [Include] attribute in your RIA domain service metadata.
[MetadataTypeAttribute(typeof(MyTestObject.MyTestObject_Metadata))]
public partial class MyTestObject
{
internal sealed class MyTestObject_Metadata
{
// Metadata classes are not meant to be instantiated.
private MyTestObject_Metadata()
{ }
[Include]
public AnotherObject Foo { get; set; }
}
}
You also need to include the references in your query.
var results = this.ObjectContext.MyTestObject.Include(Foo);
Hope this helps.
精彩评论