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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:57:06+00:00 2026-06-13T14:57:06+00:00

Entity Framework 5.0 Code first with existing database. Reverse engineered the classes with power

  • 0

Entity Framework 5.0 Code first with existing database. Reverse engineered the classes with power tools. Everything is working great. Database has two tables. One parent and one child with foreign key back to parent ID. ID’s are all int with auto increment. I’ve added a number of parent records and now want to add child records to specific parents. The only way I can see to do this is find the appropriate parent ID by searching the Parents table for a name or some other property and return the ID. Then use that ID in the foreign key property when adding the child. I don’t want to crate a new parent so is this the only way to add a child to an existing parent? All of the internet examples seem to add a child along with adding a new parent and not add a child to an existing parent. Any help would 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-06-13T14:57:07+00:00Added an answer on June 13, 2026 at 2:57 pm

    “Julie” (Lerman) happens to be one of our teachers when it comes to EF. As she explains in her book DbContext there can be three properties involved in a parent-child association, two navigation properties (parent.Children, child.Parent) and a foreign key property (child.ParentId). You can set one of these or any combination of them in your code.

    When EF is triggered to detect changes (e.g. when SaveChanges is invoked) it starts a process called relationship fixup that ensures that the three properties will be in sync.

    So you’ve got three options to add a child entity to a parent:

    1. child.Parent = parentObject. The parent object should be fetched from the database first.
    2. parentObject.Children.Add(child). Also requires a pre-existing parent object from the database.
    3. child.ParentId = parentId, for which you only need to know the id. You can get the Id the way you suggested but just as well, for objects that you know will not be deleted, you can have it saved in some variable to reuse it across transactions.

    With the first two options the child object does not need to be added to the context manually. EF knows enough to see that it’s new. With the third option it does have to be added to context.Children before SaveChanges.

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

Sidebar

Related Questions

I'm trying to use the Entity Framework code first without an existing database. I
I am using Entity Framework 4.3 code first using an already existing database. There
I use Entity Framework code-first. DataBaseName - I want to create a database in
I have an issue in entity framework code first, three table in database as
I am using Entity Framework 4.1 code first to connect to an already existing
I'm creating a database using entity framework code first and I'm having some issues
How can I use the database view in entity framework code first,
I've successfully mapped an Entity Framework Code-First data model with an existing Sql Server
Entity Framework Code First will auto-create a table in the database base based on
I'm using Entity Framework code first and pulling some data back from our database.

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.