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

  • Home
  • SEARCH
  • 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 570153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:22:15+00:00 2026-05-13T13:22:15+00:00

I have a Linq to SQL EntitySet with a Foreign Key Relationship between two

  • 0

I have a Linq to SQL EntitySet with a Foreign Key Relationship between two tables. The tables in question are a Task table (called Issues) and a Departments Table. The foreign key is the department name (Which is guaranteed unique). I am running into an issue in that you cannot change a Linq to SQL FK field if the corresponding data is loaded.

The ViewModel (Not true MVVM, I am trying to practice the concepts while learning xaml, WPF and Linq-SQL)

public class StatusBoardViewModel : INotifyPropertyChanged
{
    OIConsoleDataContext db = new OIConsoleDataContext();

    private IQueryable<Issue> issues;
    public IQueryable<Issue> Issues
    {
        get { // Lazy load issues if they have not been instantiated yet
            if (issues == null) {
                QueryIssues();
            }
            return issues; 
        }
        set {
            if (issues != value) {
                issues = value;
                OnPropertyChanged("Issues");
            }
        }
    }

    private IQueryable<Department> departments;
    public IQueryable<Department> Departments
    {
        get {
            // Lazy load departments if they have not been instantiated yet
            if (departments == null) {
                QueryDepartments();
            }
            return departments;
        }
        set {
            if (departments != value) {
                departments = value;
                OnPropertyChanged("Departments");
            }
        }
    }

    private void QueryDepartments()
    {
        Departments = from d in db.Departments
                      orderby d.DeptName
                      select d;
    }

    public void QueryIssues()
    {
        Issues = from i in db.Issues
                 where i.IssIsOpen == true
                 orderby i.IssDueDate
                 select i;
         // ....
    }

    #region INotifyPropertyChanged Members
}

My combo Box:

<ComboBox x:Name="DeptComboBox" 
  ItemsSource="{Binding Departments}" 
  DisplayMemberPath="DeptName"
  SelectedValuePath="DeptName"
  SelectedValue="{Binding Path=Issues/IssDepartment, Mode=TwoWay}"
  Grid.Column="2" Grid.ColumnSpan="2" Grid.Row="3" Margin="6,7,115,5"
  IsSynchronizedWithCurrentItem="True" /> 

As it stands it displays the combo box fine and defaults the selection to the correct department BUT if you make a change it throws an exception ‘System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException’

I tried adding

   SelectionChanged="DeptComboBox_SelectionChanged" 

to the combo box then using the following code behind to remove the department reference then add the new one. It seemed to work fine but I discovered that if the filter settings caused the linq query to be changed then the current item before the change gets its department changed to the value of the new default items department when the Selection_Changed event fires. I could not find a way to tell if the selection changed event came from the user rather than the binding property to prevent this. Besides that I was not happy with the use of code where it seems it should be in a binding or at least some kind of value converter.

// This was an attempt to work around that did not work
// The referneces are passed from the Window1 event handler
public void ChangeDepartment(Issue currentIssue, Department currentDept)
{
    if (currentIssue != null) {
        currentIssue.Department = null;
        currentIssue.Department = currentDept;
    }
}

I have to be honest with myself, I am in over my pay grade here but that’s when you learn the most eh..

How should I handle this? I have read a dozen articles on the issue and found them to be conflicting and as clear as mud.

Thanks for all your help SO

Mike

Update
I have had a very useful discussion with Chris Nicol below and he would probably already have the checkmark if I had a better handle on things. I think my biggest problem is that I am having to learn SQL, Linq, WPF and Database Design all at once. I have excellent books on all the subjects but they don’t cover interoperation. For instance Pro Linq in C# 2008 is great… except it doesn’t talk at all about Linq in WPF,

I have tried to do things as “MVVMish” as possible but, after a few false starts earlier, I decided it was one thing too many to learn at once. In practice I think I am close to MVVM except there is no commanding, I execute methods in the ViewModel class from the event handlers in the code behind. I am assuming that will make it fairly easy to refactor in the commanding structure later. My App is structured as follows:

The Model

  • Linq to SQL dbml files
  • partial classes for DataErrorInfo validation

The ViewModel

  • The DataContext
  • IQueryable collections for Tasks, Employees and Departments
  • A selected task property that is used by the add/edit windows view
  • Some properties for the view to bind to such as ShowDetailListItems() that triggers a different listbox ItemTemplate and is itself bound to a checkbox.
  • Methods to perform querys
  • Methods to open a window to add or edit a task and to submitChanges() upon return from said windows.

The View is bound to the viewmodel and the code behind contains:

  • a property that holds an instance of
    the viewmodel class
  • code to instantiate the viewmodel in the contructor and to set the datacontext to the viewModel
  • The remainder of the code behind is event handlers that will be replaced by commands eventually

I have read Josh Smiths excellent article many times and explored the sample code. most of my format is based on that. The thread listed by Chris (Below) has a ton or new material for me to explore so I will spend some time digging into that and trying to determine how best to refactor things. This has gone far beyond some ComboBoxes not working, In fact part of the original issue was that the combo boxes were being bound to two different data contexts. Until I have a manageable architecture I think I am doing more harm that good trying to patch things up.

I will probably be back later with a checkmark for Chris

Grrr: “An attempt has been made to Attach or Add an entity that is not new, perhaps having been loaded from another DataContext…”

  • 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-05-13T13:22:15+00:00Added an answer on May 13, 2026 at 1:22 pm

    I would recommend separating your data access layer from your ViewModel and using an observable collection. Once you leverage MVVM better this sort of thing isn’t that hard.

    I suggest that you read Josh Smith’s article on MVVM, it will give you a great insight to WPF and it’s binding capabilities. Accomplishing what you’re trying to do in WPF is quite easy once you understand how it’s working.

    Good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 374k
  • Answers 374k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In a query, comparing a list property to a value… May 14, 2026 at 7:57 pm
  • Editorial Team
    Editorial Team added an answer i face same problem before i can suggest start reading… May 14, 2026 at 7:57 pm
  • Editorial Team
    Editorial Team added an answer Looks like your gemrc file could be broken. You could… May 14, 2026 at 7:57 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.