Binding parent and child data into DataGrid in ria services for silverlight 4

风流意气都作罢 提交于 2019-12-25 01:33:33

问题


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 theres 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


回答1:


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.



来源:https://stackoverflow.com/questions/3799684/binding-parent-and-child-data-into-datagrid-in-ria-services-for-silverlight-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!