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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:22:43+00:00 2026-05-26T09:22:43+00:00

I am working with VS2010, .NET 4.0, Entity Framework 4, and POCO entities. I

  • 0

I am working with VS2010, .NET 4.0, Entity Framework 4, and POCO entities. I am trying to add and remove child entities from a parent. I have a many-to-many relationship between [User] and [Companies]. So I have the following entity:

// Objects created by POCO template generator
public class User
{
    public int Id { get; set; }
    public string Username { get; set; }
    public ICollection<Company> Companies { get; set; }
}
public class Company
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<User> Users { get; set; }
}

At this point in the code, the User exists but has ZERO companies. What I am trying to do is add a new child Company to the User. This code DOES save the Company but it is also creating a duplicate User parent.

public UserRepository
{
    internal ObjectContextEntities _dbContext = new ObjectContextEntities();
    public User Save(User pocoObject)
    {
        // Load the existing User which has no companies
        var loadedEntity = _dbContext.Users.Where(m => m.Id == pocoObject.Id).FirstOrDefault();

        // Add the new company from a POCO object
        loadedEntity.Companies.Add(pocoObject.Companies.Last());

        _dbContext.SaveChanges();
    }
}

How should I be adding child entities? (Without creating a duplicate relationship?) … Also is there a simple way to update those children when performing “ApplyCurrentValues()” on the parent User?

  • 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-26T09:22:44+00:00Added an answer on May 26, 2026 at 9:22 am

    I tried a more simplified approach and I figured it out:

        var newCompany = pocoObject.Companies.Last();
        newCompany.Users = null;
        loadedUser.Companies.Add(newCompany);
    

    It turns out because there was a reference to the parent INSIDE the child, it was propagating up and causing a new parent to be created. So from a POCO standpoint, I had to remove the parent and keep it a very simple object. (Or I could have just removed the Child’s Parent reference in the edmx). But even this makes my life easier:

        loadedUser.Companies.Add(new Company() { Name = "New Company" });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on project (VS2010 .NET) and have two SQL Server databases, Dev
I am working with Entity Framework 4.0, VS 2010, MySql server database, and mysql-connector-net-6.4.4
I am working with VS 2010, Entity framework, SQl-Server 2005, ASP.Net web forms .
I am working with Entity Framework 4.0 (VS 2010 Beta 2, NOT RC). I
Working with an Oracle 9i database from an ASP.NET 2.0 (VB) application using OLEDB.
I'm working on a .Net application that needs to read from an Oracle 10g
Development Environment : - VS2010 - .Net Framework 4.0, 3.5, 2.0 Staging and Production
I am working on VS2010 C# and I want to add a .dll reference
I'm working on a web-based academical evaluation project (VS2010,C#,ASP.NET). Users can enter the web
I am working in VS2010 ASP.NET, I decided to move some of my pages

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.