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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:22:52+00:00 2026-05-26T02:22:52+00:00

I’ve been trying for a while to debug this and express what I want

  • 0

I’ve been trying for a while to debug this and express what I want to do in different ways, but am having no luck. I don’t know a whole lot about the entity framework, and I inherited this code from another developer who’s no longer around so I don’t know a whole lot about the model. Perhaps someone can at least get me pointed in the right direction. Here’s my code:

//Append note to project notes too
task.TPM_PROJECTVERSIONReference.Load();
TPM_PROJECTVERSIONNOTES newNote = new TPM_PROJECTVERSIONNOTES();
newNote.TS = System.DateTime.Now;
newNote.TPM_USER = SessionHandler.LoginUser;
newNote.NOTES = task.NOTES;
newNote.PROJECTID = this.ProjectId;
newNote.VERSIONID = this.VersionId;
task.TPM_PROJECTVERSION.TPM_PROJECTVERSIONNOTES.Add(newNote); //<-- Exception

I have “task”, which points to a project version it belongs to. These projects have a bunch of notes. I want to add a new note to the task’s project. However, I get an exception on the last line:

System.InvalidOperationException was unhandled by user code
Message=The relationship between the two objects cannot be
defined because they are attached to different ObjectContext objects.
Source=System.Data.Entity

This seems like it should be a pretty simple thing. Is this not the right way to do this? Thanks!

Edit:

Ok I’ve removed ALL other code from the function and created a brand new context. Here’s the entire function now from start to finish:

//Append note to project notes too
using (TPMEntities context = new TPMEntities(General.EntityName()))
{
   TPM_PROJECTVERSIONNOTES newNote = context.TPM_PROJECTVERSIONNOTES.CreateObject();
   newNote.TS = System.DateTime.Now;
   newNote.TPM_USER = SessionHandler.LoginUser;
   newNote.NOTES = this.txtTaskNotes.Text;
   TPM_PROJECTVERSION version = (from pv in context.TPM_PROJECTVERSION
                                 where pv.PROJECTID == this.ProjectId && pv.VERSIONID == this.VersionId
                                 select pv).First();

   version.TPM_PROJECTVERSIONNOTES.Add(newNote);
   context.SaveChanges();
}

Still, I get the same exception:

System.InvalidOperationException was unhandled by user code
  Message=The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects.
  Source=System.Data.Entity

I simply can’t understand how there can be two contexts? I’m using “context” every single place in the code. How can the Entity Framework make it so difficult to simply add a single row into my database?

  • 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-26T02:22:52+00:00Added an answer on May 26, 2026 at 2:22 am

    The problem is in

    newNote.TPM_USER = SessionHandler.LoginUser;
    

    LoginUser is created using another context. You are attaching that to context. There are couple of ways to solve this.

    Detach the LoginUser from the context it was created.

    sessionContext.Detach(SessionHandler.LoginUser);
    

    Then inside your method

    using (TPMEntities context = new TPMEntities(General.EntityName()))
    {
       context.Attach(SessionHandler.LoginUser);
    
       TPM_PROJECTVERSIONNOTES newNote = context.TPM_PROJECTVERSIONNOTES.CreateObject();
       newNote.TS = System.DateTime.Now;
       newNote.TPM_USER = SessionHandler.LoginUser;
       newNote.NOTES = this.txtTaskNotes.Text;
       TPM_PROJECTVERSION version = (from pv in context.TPM_PROJECTVERSION
                                     where pv.PROJECTID == this.ProjectId && pv.VERSIONID == this.VersionId
                                     select pv).First();
    
       version.TPM_PROJECTVERSIONNOTES.Add(newNote);
       context.SaveChanges();
    }
    

    Or if you have mapped the scalar foreign key property newNote.TPM_USER_ID

    using (TPMEntities context = new TPMEntities(General.EntityName()))
    {
       TPM_PROJECTVERSIONNOTES newNote = context.TPM_PROJECTVERSIONNOTES.CreateObject();
       newNote.TS = System.DateTime.Now;
       newNote.TPM_USER_ID = SessionHandler.LoginUser.ID;
       newNote.NOTES = this.txtTaskNotes.Text;
       TPM_PROJECTVERSION version = (from pv in context.TPM_PROJECTVERSION
                                     where pv.PROJECTID == this.ProjectId && pv.VERSIONID == this.VersionId
                                     select pv).First();
    
       version.TPM_PROJECTVERSIONNOTES.Add(newNote);
       context.SaveChanges();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I want to construct a data frame in an Rcpp function, but when I
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

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.