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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:54:44+00:00 2026-06-17T06:54:44+00:00

I have a batch work, I need to add new relations between existing entities,

  • 0

I have a batch work, I need to add new relations between existing entities, the entities have big data, I can’t get the entities and add them, I have tried this:

  for .........
                Class Class = new Class() { ClassID = myClassId };
                context.AddToClasses(Class);
                Student std = new Student() { Id = myStdId };
                context.AddToStudents(std);
                context.ObjectStateManager.ChangeObjectState(Class, System.Data.EntityState.Unchanged);
                context.ObjectStateManager.ChangeObjectState(std, System.Data.EntityState.Unchanged);
                Class.ClassStudents.Add(std);

  .............

The first call will success, but the second one will give me this exception:

AcceptChanges cannot continue because the object’s key values conflict
with another object in the ObjectStateManager. Make sure that the key
values are unique before calling AcceptChanges.

Tried :

  for .........
                Class Class = new Class() { ClassID = myClassId };
                context.Attach(Class);
                Student std = new Student() { Id = myStdId };
                context.Attach(std);
                context.ObjectStateManager.ChangeObjectState(Class, System.Data.EntityState.Unchanged);
                context.ObjectStateManager.ChangeObjectState(std, System.Data.EntityState.Unchanged);
                Class.ClassStudents.Add(std);

  .............

Exception:

An object with a null EntityKey value cannot be attached to an object
context.

How can I do this?

  • 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-17T06:54:45+00:00Added an answer on June 17, 2026 at 6:54 am

    If the pairs of myClassId and myStdId can be repetitive and in no particular order in the loop you are running over this code I would use two dictionaries to hold the already created and attached entities to avoid that you create and attach an entity with the same key a second time, like so:

    var classDict = new Dictionary<int, Class>();
    var studentDict = new Dictionary<int, Student>();
    
    // for ... loop starts here
    
    Class Class;
    if (!classDict.TryGetValue(myClassId, out Class))
    {
        Class Class = new Class() { ClassID = myClassId };
        context.Classes.Attach(Class);
        classDict.Add(myClassId, Class);
    }
    
    Student std;
    if (!studentDict.TryGetValue(myStdId, out std))
    {
        Student std = new Student() { Id = myStdId };
        context.Students.Attach(std);
        classDict.Add(myStdId, std);
    }
    
    Class.ClassStudents.Add(std);
    
    // loop ends here
    
    context.SaveChanges();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have batch file that I am inheriting and I cannot get to work
Since batch doesn't really have ints, I have to work around it with set
Note: I have almost no idea at all how batch file 'for' loops work.
I have a batch file that I need to run within my NSIS installer.
I have a series of XML files which I need to add a few
I have an xml batch file that will be transferred between two parties over
Quite simply, I need to store time series data in a document. I have
I have a script that I need to execute using a batch file. Do
I have a batch of reports that are set up to print very nicely
I have a batch script that outputs a file, and I'm trying to ensure

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.