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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:40:06+00:00 2026-06-12T03:40:06+00:00

I have an Entity Framework model that has 3 tables (each with indentity primary

  • 0

I have an Entity Framework model that has 3 tables (each with indentity primary keys). The root table has a 1 to many relationship to a child table, that child table has a 1 to many relationship with it’s child table. This model is reflected correctly in the model that was generated from the database.

In code, we do an Insert (Add) into the parent table, we then do an insert of that children’s tables, and then finally we do inserts on the children’s children. The code looks similar to the example below:

foreach(var parentItemDTO in someDTOCollection) {
    foreach(var someChildDTOItem in someChildDTOCollection) {
        // Do some mapping here to the childEntity from DTO
        // The foreign key relationship isn't set during mapping.

        childTable.Insert(childEntity); // Underlying code is _dbSet.Add(entity)

        foreach(var someChildChildDTOItem in someDTOChildChildCollection) {
            // Do some mapping here to childChildEntity from DTO
            // The foreign key relationship isn't set during mapping.

            childChildTable.Insert(childChildEntity);   // Underlying code is _dbSet.Add(entity)
        } 
    }

    // Do mapping here of the parentEntity from DTO

    parentTable.Insert(someEntity);  // Underlying code is _dbSet.Add(entity)
}

The inserts into the database seem to be working. However, what I would like to understand is under the hood how does EF maintain the relationship of these objects without me explicitly defining the foreign key relationship during the mapping? Is these inserts scope safe? Will this cause orphans or children being inserted into the wrong parent (right now we don’t see this happening but does it have the potential)?

Thanks!

EDITED (CORRECTION):

The code has been updated to reflect that the parent insert is happening AFTER all the children inserts.

  • 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-12T03:40:07+00:00Added an answer on June 12, 2026 at 3:40 am

    For the entities to be tracked properly by EF you need to have properties that represent relationships between entities. You parent entity should have a property referencing children and children in turn should have properties referencing their children. For example:

    class ParentEntity {
        public int Id { get; set; }
        public ICollection<ChildEntity> Children { get; set; }
    }
    
    class ChildEntity { 
        public int Id { get; set; }
    }
    

    As long as you add child entities to parent’s Children collection EF can keep track of relationships:

    var parent = new ParentEntity();
    parent.Children.Add(new ChildEntity());
    parent.Children.Add(new ChildEntity());
    

    EF knows that object references in parent.Children collection represent new entities (entities not attached to the context) and will handle them accordingly. The actual inserts to the database don’t happen until you call SaveChanges(). When you add an object to DbSet EF just begins to keep track of it in memory. Only when you call SaveChanges() entities will be written to the database. At this point EF will figure out that parent entity needs to be saved first. It will then use parent entity’s PK as FK in your child entities. Now you can add parent to context and this will also add children:

    context.Set<ParentEntity>().Add(parent);
    context.SaveChanges(); // adds parent and two children.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have an entity framework model with has some inheritance in it. The following
I have a simple ADO.NET Entity Framework 4.0 model (edmx) which defines database tables
I have an Entity Framework generated table with values in a td that I
I have an entity (Org) in my Entity Framework that has a foreign key
I have an Entity Framework model that was generated by the Visual Studio 2008
I have 2 tables that are mapped in my entity model which are basically
I have 4 tables: but when I create an entity framework model, why is
I have an Entity Framework data model. Part of the model is a Customer
I have created a Dynamic Data site against an Entity Framework Model I have
I'm working with a small model in Entity Framework 4.0. I'd like to have

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.