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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:54:07+00:00 2026-05-21T11:54:07+00:00

In a situation where you have a parent class which has one child class,

  • 0

In a situation where you have a parent class which has one child class, what is the best pattern for mapping the entities.

I have seen a lot of suggestions where the parent has a reference to both the child class, and the child class id. E.g.

public class Parent
{
    public int Id
    {
        get;
        set;
    }

    public int ChildId
    {
        get;
        set;
    }

    public virtual Child Child
    {
        get;
        set;
    }
}

public class Child
{
    public int Id
    {
        get;
        set;
    }
}

public class ParentMapping : EntityTypeConfiguration<Parent>
{
    public ParentMapping()
    {
        HasKey(x => x.Id);

        HasRequired(X => x.Child)
            .WithMany()
            .Map(x => x.ToTable("Parent")
                        .MapKey("ChildId"));
    }
}

With this pattern, when saving the parent, if you want to swap out the child for a different but existing child, examples I have seen just update the ChildId and not the Child which feels wrong because the object is out of sync with itself.

The code looks neater without the ChildId but with this pattern I am having trouble saving the parent using an existing child because EF is trying to save a new child.

public class Parent
{
    public int Id
    {
        get;
        set;
    }

    public virtual Child Child
    {
        get;
        set;
    }
}

What is the best pattern, I would like to know if the ChildId is needed, then how is the Child property kept in sync and will it be lazy loaded from the database or not.

  • 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-21T11:54:08+00:00Added an answer on May 21, 2026 at 11:54 am

    This is difference between foreign key and independent association. When using foreign key association you can really use just key without loading the related object. It makes the reference out of sync if you have it loaded – which is not always the case. If you want to keep the reference in sync you are almost back in the situation which you must solve with independent association.

    If you expose foreign key you should use it because it makes a lot of things much more easier. If you use the independent association you should do something like:

    var parent = GetUpdatedParentSomehow();
    // Dummy object for the old child if the relation is not loaded
    parent.Child = new Child { Id = oldChildId }; 
    // Attach the parent
    context.Parents.Attach(parent);
    
    // Create dummy for new child (you can also load a child from DB)
    var child = new Child { ID = newChildId };
    // No attach the child to the context so the context
    // doesn't track it as a new child
    context.Childs.Attach(child);
    // Set a new child
    parent.Child = child;
    // Set parent as modified
    context.Entry(parent).State = EntityState.Modified;
    context.SaveChanges();
    

    There is very strange part where I’m creating dummy for the old child. I’m almost sure that if I don’t do it before attaching the parent and setting the new child I will get some exception during saving changes (in case of independent association).

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

Sidebar

Related Questions

I have a parent thread (non-UI) which creates some child threads to do some
The situation: I have a pieceofcrapuous laptop. One of the things that make it
My situation: I have several components, which sometimes have changes to them, and are
Does anyone have any suggestions for a best practice or preferred way of rolling
Situation: I have a simple XML document that contains image information. I need to
I have following situation: I have loged user, standard authentication with DB table $authAdapter
I have the following situation: I have a certain function that runs a loop
My Situation I have a N rectangles The rectangles all have the same shape
Here's the situation: I have a label's text set, immediately followed by a response.redirect()
Here is the situation: I have been called upon to work with InstallAnywhere 8,

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.