locked
Bind Entity reference table data to control RRS feed

  • Question

  • Hi

    I am doing cascading on two comboboxes.
    I have tables Country and City in database. City is the child table of Country table. (Have implemented Entity Framework in application).

    <ComboBox ItemsSource="{Binding Countries}" SelectedItem="{Binding SelectedCountry, Mode=TwoWay}" DisplayMemberPath="CountryName"/>
    
    <ComboBox ItemsSource="{Binding SelectedCountry.Cities}" SelectedItem="{Binding SelectedCity, Mode=TwoWay}" DisplayMemberPath="CityName">
    

    Would like to ask is this a correct way to bind City combobox with reference table of SelectedCountry property (which is nothing but the instance of Entity Framework table Country)?
    Or do I have to create separate property (may be "Cities") for this?

    private ObservableCollection<Country> _Countries;
            public ObservableCollection<Country> Countries
            {
                get { return _Countries; }
                set { Set(ref _Countries, value); }
            }
    
    Country _SelectedCountry;
            public Country SelectedCountry
            {
                get { return _SelectedCountry; }
                set { Set(ref _SelectedCountry, value); }
            }

    Somnath

    Thursday, January 14, 2016 7:21 AM

Answers

  • >>Would like to ask is this a correct way to bind City combobox with reference table of SelectedCountry property (which is nothing but the instance of Entity Framework table Country)?

    Yes, as long as the Cities collection of the selected Country object is actually populated with City objects by the time a country is selected.

    As you have already figured out, you could use eager loading to load the related City object upfront. Please refer to the following page on MSDN for more information about this: https://msdn.microsoft.com/en-us/data/jj574232.aspx

    Lazy loading is only meaningful in situations where you have a long-lived context that is still around when a new country gets selected in the first ComboBox. I would prefer to load the related City entities upfront using eager loading:

    context.Counties.Include(t=> t.City);

    I have an example of how you could implement cascading ComboBoxes in WPF that you might find helpful:  http://blog.magnusmontin.net/2013/06/17/cascading-comboboxes-in-wpf-using-mvvm/

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Thursday, January 14, 2016 7:20 PM

All replies

  • I don't see any problem in binding

    <ComboBox ItemsSource="{Binding SelectedCountry.Cities}" 

    With a combo, you're not going to be editing the data so there's no point in wrapping with viewmodels unless you have a specific requirement.

    Have you found a particular issue with this?


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Thursday, January 14, 2016 10:02 AM
  • I have not disabled EF Lazy Loading anywhere in application.

    First time when I load SelectedCountry, child table City gets loaded with simple EF query (without including City table), but after saving that record in database if I read SelectedCountry, then City table is not getting loaded (while the reference value for City is stored correctly in database).

    I checked particularly in debug mode.

    I have to use .include(t=> t.City) in EF query to load that table.


    Thursday, January 14, 2016 10:34 AM
  • I don't follow.

    Yes, if you read countries then you need to .Include("Cities") in your linq.

    You bind selecteditem of countries

    SelectedItem="{Binding SelectedCountry, Mode=TwoWay}"

    And use that selectedCountry to provide the itemssource for the cities combo.

    The user will have to select a country out the country combo before the cities combo will be filled.

    Where are you seeing a problem?

    And what is the problem?


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Thursday, January 14, 2016 11:06 AM
  • >>Would like to ask is this a correct way to bind City combobox with reference table of SelectedCountry property (which is nothing but the instance of Entity Framework table Country)?

    Yes, as long as the Cities collection of the selected Country object is actually populated with City objects by the time a country is selected.

    As you have already figured out, you could use eager loading to load the related City object upfront. Please refer to the following page on MSDN for more information about this: https://msdn.microsoft.com/en-us/data/jj574232.aspx

    Lazy loading is only meaningful in situations where you have a long-lived context that is still around when a new country gets selected in the first ComboBox. I would prefer to load the related City entities upfront using eager loading:

    context.Counties.Include(t=> t.City);

    I have an example of how you could implement cascading ComboBoxes in WPF that you might find helpful:  http://blog.magnusmontin.net/2013/06/17/cascading-comboboxes-in-wpf-using-mvvm/

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Thursday, January 14, 2016 7:20 PM