OneFineDay
OneFineDay

Reputation: 9024

Binding to navigation property

I am using Binding on the DataGrid columns and have used the Navigation Properties in the past with expected results. I am getting the other fields to show just not these two that use Navigation Properties. The properties are spelled correctly as for the fields they represent. I am using Entity Framework for backend via WCF. When I check the CalenderChange object that is returning via the WCF function, I can see the Navigation Property and the readonly field is _Firstname and has the value I expect. I tried changing the Binding to Binding="{Binding Personnel._Firstname} without success.

Include it in the query:

db.CalenderChanges.Include("Personnel").Where({condition}).ToList()

XAML:

<sdk:DataGridTextColumn Header="First Name" Binding="{Binding Personnel.Firstname}"  Width="100" />
<sdk:DataGridTextColumn Header="Last Name" Binding="{Binding Personnel.Lastname}"  Width="100" />

What am I missing? Thanks...

Upvotes: 0

Views: 115

Answers (1)

Chui Tey
Chui Tey

Reputation: 5554

If you are using WCF Ria Services, the usual reason is because using Include("Personnel") is not sufficient.

For Silverlight to get the Personnel data, you need both conditions

  1. db.CalenderChanges.Include("Personnel") so that Personnel is returned from database

  2. The metadata for Personnel navigation property is marked with [Include] attribute, so that Personnel is marshalled by WCF to the client. see https://stackoverflow.com/a/5332188/34461

sample

// ExtraMetadata.cs
// Add Reference to System.ComponentModel.DataAnnotations.dll

namespace MyModel
{
  using System.ComponentModel.DataAnnotations;

  // This attribute tells Ria Services that the metadata
  // for the CalendarChange class is in CalenderChange.MetaData class
  [MetadataType(typeof(CalenderChange.MetaData)]
  public partial class CalenderChange {
    public class MetaData {

        // adding this attribute tells RIA services 
        // to also send this property across the wire
        [Include]
        public MetaData Personnel { get; set; }
    }
  }
}

Reference:

  1. MSDN How to add metadata classes.

  2. MSDN IncludeAttribute

Upvotes: 2

Related Questions