none
Entity Framework Binding to 'sub' list

    Question

  • Hi all,

    This is my first post so I hope it's going to the right place...

     

    I am developing a Contact Management Application using the Entity Framework, C# and WPF.

    At the moment I have a WPF ListView that displays the name of each contact company from a ObjectSet returned from the Entity Framework. Beside this I have a number of text fields that display the details for the selected company. So far this is working fine, I can bind to my collection of companies and display the member details like so:

    <Label>Company:</Label>
    <TextBox x:Name="companyGeneralCompNameBox">
      <Binding ElementName="companiesView" Path="SelectedItem.txtCompanyName"/>
    </TextBox>
    

    The problem I am having is that each company has a list of people within it that I need to display in a further ListView. The Entity Model shows tblCompany has a navigation property called tblPerson_PersonRole_Company (this is a link table between tblCompany and tblPerson). I have tried binding to this as follows but I'm not having much luck:

    <ListView x:Name="companyContactsList" ItemsSource="{Binding ElementName=companiesView, Path=SelectedItem}" Grid.Column="0" Grid.Row="0">
     <ListView.View>
      <GridView>                
       <GridViewColumn Width="auto" Header="Primary" DisplayMemberBinding="{Binding Path=tblPerson_PersonRole_Company.tblPerson.txtPersonRole}" />
       <GridViewColumn Width="auto" Header="Name" DisplayMemberBinding="{Binding Path=tblPerson_PersonRole_Company.tblPerson.txtSurname}" />
       <GridViewColumn Width="auto" Header="Phone" DisplayMemberBinding="{Binding Path=tblPerson_PersonRole_Company.tblPerson.numPhone}" />
      </GridView>
     </ListView.View>
    </ListView>
    

    Do I need to do something a little more clever? Have I missed the point entirely? I'm pretty new to the whole Entity Framework thing so any help would be great.

    Many thanks,

    Paul

    • Moved by Yves.Z Tuesday, March 29, 2011 3:22 PM EF related (From:Windows Presentation Foundation (WPF))
    Thursday, March 24, 2011 1:04 PM

Answers

  • Try and do this for your query;

    companies = contactsContext.tblCompanies.Include("tblPerson");

    The error means you are trying to bind a source property and a target property that don't match.  So somewhere you are binding tblCompany to a target that can't convert from your tblComany type to its target type.

     

    Thursday, March 24, 2011 5:12 PM

All replies

  • Probably not the best place to ask, there is an Entity Framework forum:

    http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/threads/

    But that being said, have you verified that you are including the child tables in your queries?  Depending on your lazy load configuration, they are not loaded by default.  You have to either explicitly load them with a separate query, or use .Include("YourChildTable").

    If those aren't being loaded here's a link that describes what to do:

    http://msdn.microsoft.com/en-us/library/bb896272.aspx

    Hope that helps!


    Thursday, March 24, 2011 3:58 PM
  • Hi Bryan,

    Thanks for the reply. I'll look into the lazy loading stuff.

    In the code file I'm loading my list of companies with the following code:

    contactsContext = new t2eContactsEntities();
    companies = contactsContext.tblCompanies;

    This gives me an ObjectSet<tblCompany> that I can expose as a property. Does that seem like the right way to do this?

     

    I've also noticed the following error in the debugger that I'm struggling to diagnose:

    System.Windows.Data Error: 6 : 'ObjectSourceConverter' converter failed to convert value 't2e.contacts.data.tblCompany' (type 'tblCompany'); fallback value will be used, if available. BindingExpression:Path=SelectedItem; DataItem='ListView' (Name='companiesView'); target element is 'ListView' (Name='companyContactsList'); target property is 'ItemsSource' (type 'IEnumerable') NotSupportedException:'System.NotSupportedException: ReferenceConverter cannot convert from t2e.contacts.data.tblCompany.
      at MS.Internal.Data.DefaultValueConverter.ConvertHelper(Object o, Type destinationType, DependencyObject targetElement, CultureInfo culture, Boolean isForward)
      at MS.Internal.Data.ObjectSourceConverter.Convert(Object o, Type type, Object parameter, CultureInfo culture)
      at System.Windows.Data.BindingExpression.ConvertHelper(IValueConverter converter, Object value, Type targetType, Object parameter, CultureInfo culture)'

     Do I maybe need to use some sort of converter to load the list of people?

     

    Thanks again for getting back to me,

    Paul

    Thursday, March 24, 2011 4:22 PM
  • Try and do this for your query;

    companies = contactsContext.tblCompanies.Include("tblPerson");

    The error means you are trying to bind a source property and a target property that don't match.  So somewhere you are binding tblCompany to a target that can't convert from your tblComany type to its target type.

     

    Thursday, March 24, 2011 5:12 PM