Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8847685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:14:56+00:00 2026-06-14T12:14:56+00:00

I am having some trouble with data binding and the Entity Framework’s navigation properties.

  • 0

I am having some trouble with data binding and the Entity Framework’s navigation properties.

I have two classes, generated by the Entity Framework designer:

Class Foo:

id (int)
bar (Bar)
...

Class Bar

id (int)
name (string)
...

Using an ObservableCollection<Foo>, I have populated a datagrid with the following columns:

<DataGrid.Columns>
    <DataGridTextColumn Header="Id" Binding="{Binding Path=id}"/>
    <DataGridTemplateColumn Header="Bar">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <ComboBox 
                    SelectedValuePath="Id" 
                    SelectedValue=
                        "{Binding Path=bar.Id, Mode=TwoWay,
                        UpdateSourceTrigger=PropertyChanged}"
                    DisplayMemberPath="name" 
                    ItemsSource=
                        "{Binding Path=BarList, 
                        RelativeSource={RelativeSource FindAncestor, 
                        AncestorType={x:Type Window}}}"
                    Background="White" />
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
</DataGrid.Columns>

The ComboBox is populated with an ObservableCollection<Bar> and is correctly showing the current Bar.

The problem comes when I select another item in the combobox. I get the following error:

System.Windows.Data Error: 8 : Cannot save value from target back to source.

System.InvalidOperationException: The property 'Id' is part of the object's key information and cannot be modified

I can see why the error pops up, but how can I handle this differently?

EDIT: The relationship between Foo and Bar is N..1, meaning that a Foo has 1 or 0 Bar while a Bar can have several Foos.

At the moment, I am not able to select a new Bar for my Foos.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T12:14:57+00:00Added an answer on June 14, 2026 at 12:14 pm

    You have to bind on the bar property directly. Your code tries to change the Id on the selected Foo’s bar, but what you want is to change the bar associated to the current Foo. You also have to override Equals method on your Bar class.

    <ComboBox SelectedValue="{Binding bar, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
              DisplayMemberPath="Name"
              ItemsSource="{Binding Path=BarList, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" Background="White" />
    

    And in your Bar class :

    public override bool Equals(object obj)
    {
        if (obj is Bar)
            return ((Bar)obj).Id == Id;
        return false;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some trouble indexing data.frames in R. I'm an R beginner. I have
I'm having trouble with some Excel data validation. On one sheet, I have a
I'm having some troubles with binding data. I have an application that contains a
I have been having some trouble loading the CheckBoxList with data. I have been
I am having trouble binding some data from my MySQL database to a DataGridView
I have just started using WPF and am having trouble data binding from the
I'm having some trouble reading data from a file into a vector of Orders.
I'm having some trouble getting my Core Data entities to play nice and order
I'm having some trouble understanding the delegate/data source methodology. I understand that they exist
I'm having trouble with some data I've imported from an XML file. Unfortunately I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.