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 1002185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:47:43+00:00 2026-05-16T07:47:43+00:00

I keep getting the following InvalidOperationException : The relationship between the two objects cannot

  • 0

I keep getting the following InvalidOperationException:

The relationship between the two objects cannot be defined because
they are attached to different ObjectContext objects.

when trying to do the following code:

newCorr.ReqCode = (from req in context.ReqCodeSet
                   where req.Code.Equals(requirement.Code)
                   select req).FirstOrDefault();

Just before this line, I am doing the following:

 foreach (Requirement requirement in myInformation.Reqs)
 {
     MyHwReqCorr newCorr = new MyHwReqCorr();

     newCorr.HwItem = Dictionaries.Instance.HwIdHwRecordDictionary[requirement.Id];

So what I’m doing is parsing through the my Information.Reqs list, creating a new instance of MyHwReqCorr, setting the HwItem to an item that was stored in a dictionary earlier on, and then setting the ReqCode by using a LINQ to SQL command which to look in a table for a req code that matches the one I’m passing in. Any help would be greatly appreciated. Any info you need, I’d be happy to provide.

EDIT: Right before I call this foreach, I can call this (as testing to verify that I can access the db):

List<ReqCode> reqCodeList = (from req in context.ReqCodeSet select req).ToList();

And I never get any errors with that. But when I try to set an item in that list (using the where extension method like:

newCorr.ReqCode = reqCodeList.Where(t=>t.Code == requirement.Code).FirstOrDefault();

or using a dictionary as done similar to the newCorr.HwItem, I get the main error.

EDIT2: I have also noticed something weird happening: When I initially run, with any setup (my original or the variable method or the method Rony posted), it works. But any subsequent run, meaning if I stop debugging and start debugging again, it fails with that error. Only when I kill all instances of excel (which is running in the background generating a log for viewing later on) and wait about 2-3 minutes, does it work again and then follows the same situation as before…passing the first time, failing immediate subsequent times.

EDIT3: It’s definitely not Excel related as I prevented Excel from starting and I still get that error. But I did notice that if I wait some time, and try again, it works….sometimes.

  • 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-16T07:47:43+00:00Added an answer on May 16, 2026 at 7:47 am

    Are you retrieving all items on the same thread/context? Try retrieving the items on same thread.

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

Sidebar

Ask A Question

Stats

  • Questions 524k
  • Answers 524k
  • 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 The way I do this, is to ignore certain extensions...… May 16, 2026 at 9:54 pm
  • Editorial Team
    Editorial Team added an answer I think you should check CMTime struct. May 16, 2026 at 9:54 pm
  • Editorial Team
    Editorial Team added an answer The only HTML escaped characters in there are &gt; and… May 16, 2026 at 9:54 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

Related Questions

This has been driving me nuts. I keep getting the following exception System.InvalidOperationException: The
I keep getting the following error: Cannot access a disposed object. Object name: 'AdoTransaction'.
I keep getting the following event log descriptions for svcListener and svcListener - Prof
I keep getting the following exception with a new resource Im making and i
I keep getting the following error, I open visual studio and after a while
i keep getting the following errror, 'ORA-01008: not all variables bound', im guessign its
I keep getting the following error when debugging. Cross-thread operation not valid: Control 'richTextBoxReceivedMsg'
I'm using Grails Mail plugin and trying to send email and keep getting: Error
Hi I am trying to commit the following value (HatMark K.K.) to the database
The following line of code is a generic version of a line in a

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.