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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:08:12+00:00 2026-05-24T07:08:12+00:00

I have some Parent records already inserted in the database. Now i want to

  • 0

I have some Parent records already inserted in the database. Now i want to add some child records. To do this I followed following steps:

  1. Retrieve the Parent(A) records
  2. Create a new child(B) record
  3. Add parent record to the Navigation property of Child. B.A = A
  4. Call SaveChanges.

The problem is when i do this EF inserts a New Parent and then add the child with a foreign key pointing to new newly inserted parent instead of inserting just the child with mapping to already existing parent. I also checked the parent’s primary key when saving the child and it does exists in the database.

Note that i am using database generated identity for Parent and Child.
One thing i noticed was if I add/Save Parent and Child from the same context object then it works fine.

Need to fix this asap. Any help will be greatly appreciated.

  • 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-24T07:08:13+00:00Added an answer on May 24, 2026 at 7:08 am

    Yes you must use the same context for that operation. That is the point. If you don’t use the same context you must manually control which object will be inserted, which updated and which will not be modified. In your scenario the sequence can be:

    context.Parents.Attach(parent); // just attach as unchanged
    context.Childs.Add(child); // add as inserted
    parent.Childs.Add(child); // make connection between existing and inserted object
    context.SaveChanges();
    

    Another approach can be:

    child.Parent = parent;
    context.Childs.Add(child); // both child and parent are marked as inserted!!!
    context.Entry(parent).State = EntityState.Unchanged; // set parent as unchanged
    context.SaveChanges();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this html: <div class=foo parent> <div class=child></div> </div> with some css: .foo{
I have a database table that holds parent and child records much like a
I have a parent thread (non-UI) which creates some child threads to do some
I have a parent thread (non-UI) which creates some child threads to do some
I have a generic list. Some elements of this list belong to a parent
I have some Oracle tables that represent a parent-child relationship. They look something like
I have a list view which is populated via records from the database. Now
I have 2 Linq2Sql classes: Parent and Child . I want to do things
Scenario: I have the following structure; Group (parent) -> Item (child). A Group can
I want to fetch some data from the following database table 'article'. i want

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.