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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:01:11+00:00 2026-05-28T19:01:11+00:00

I have quite a simple situation. I’ve got 3 objects: an object called TrainingConsultant

  • 0

I have quite a simple situation. I’ve got 3 objects:

  • an object called TrainingConsultant which is not yet saved to the database
  • an object called Training which was recently saved to the database and its EntityState is Detatched.
  • an object called Consultant which was recently loaded the from database and its EntityState is Detatched.

I need to save the TrainingConsultant object with referenced Consultant and Training, so that in the database all the required fields and foreign keys are filled in properly.

The problem is that until I call context.TrainingConsultants.AddObject(trainingConsultant);, everything is in the Detached state. After that, both training and consultant have their EntityState changed to Added, so context.SaveChanges() tries to add both training and consultant to the database before adding traingingConsultant. It fails because Consultant already exists in the database, so does consultant.

How can I fix this so both Consultant and Training won’t get saved but still get referenced? Keep in mind that I am assigning those 2 referenced objects somewhere else in code:

trainingConsultant.Training = training;
trainingConsultant.Consultant = consultant;

So when it comes to saving any changes would require me to save objects temporary, then set TrainingConsultant.Training and TrainingConsultant.Consultant to null?

using (var context = new Entity(Settings.sqlDataConnectionDetails))
{
    context.TrainingConsultants.AddObject(trainingConsultant);
    context.SaveChanges();
}
  • 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-28T19:01:12+00:00Added an answer on May 28, 2026 at 7:01 pm

    You should just specify by ID rather than attaching an entity:

    trainingConsultant.TrainingId = training.Id;
    trainingConsultant.ConsultantId = consultant.Id;
    
    context.TrainingConsultants.AddObject(trainingConsultant);
    context.SaveChanges();
    

    You can adjust as necessary to match your field/column names.

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

Sidebar

Related Questions

I have an interesting situation on my hands, and I'm not quite sure how
The question is quite simple : I have a controller plugin for Zend Framework.
My problem is quite simple: I have a 400MB file filled with 10,000,000 lines
I am stuck with something quite simple but really annoying: I have an xml
I am trying to accomplish something that seemed quite simple... I have 3 div
I have a written a very simple UDP Server using Netty - it quite
Have a look a this simple example. I don't quite understand why o1 prints
I have a problem that is common, but I am not quite sure exactly
I have a very simple JLabel with a quite long text. The parent layout
I have quite a lot of PHP view files, which I used to include

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.