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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:02:49+00:00 2026-05-23T11:02:49+00:00

I am having trouble peristing a new object graph to the context with a

  • 0

I am having trouble peristing a new object graph to the context with a one-to-many relationship. I am using the Entity Framework 4.1 release, and implementing a Code-First approach. I am using an existing SQL 2008 database and implemented a context derived from DbContext. I have two classes, Person and Address. A person can contain 0 or more Addresses, defined as such.

public class Person 
    {
        public Person()
        {
            Addresses = new List<Address>();
        }

        public int PersonId { get; set; }
        ***Additional Primitive Properties***

        public virtual ICollection<Address> Addresses { get; set; }

    }

public class Address 
    {
        public int AddressId { get; set; }
        public int AddressTypeId { get; set; }
        ***Additional Primitive Properties***

        public int PersonId { get; set; }
        public virtual Person Person { get; set; }
    }

I am trying to create a new instance of Person with two addresses. However, when I add this structure to the context and save, only the first Address in the collection is persisted. The second has the Person navigation property set to null, and is not associated with the Person object, however, the first one in the list is associated.

var person = new Person();

var mailingAddress = new Address() { AddressTypeId = 1 };
person.Addresses.Add(mailingAddress);

var billingAddress = new Address() { AddressTypeId = 2 };
person.Addresses.Add(billingAddress);

context.People.Add(entity);
context.SaveChanges();

It does not throw an exception, but the second item in the Address collection is just not saved.

Does anybody have any good ideas on why only the first would be saved? Thank you.

  • 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-23T11:02:50+00:00Added an answer on May 23, 2026 at 11:02 am

    After hours of troubleshooting/trial and error, I’ve solved my problem.
    My POCO classes are also used in a disconnected environment, where
    the objects are detached from the context, modified, and then re-attached.

    In order to determine which navigation property collection items were affected, I overrode
    the Equals and GetHashCode methods in the Address class to determine equality. Apparently this affects the ability for EF 4.1 to insert a complete collection of navigation property objects???

    Here are the original equality methods which caused the issue:

    public override bool Equals(object obj)
    {
        Address address = obj as Address;
        if (address == null) return false;
        return address.AddressId == this.AddressId;
    }
    
    public override int GetHashCode()
    {
        return this.AddressId.GetHashCode();
    }
    

    In order to correct the problem, I created a custom equality comparer
    for the navigation object rather than including it directly in the address class.

    public class AddressEqualityComparer : IEqualityComparer<Address>
    {
        public bool Equals(Address address1, Address address2)
        {
            if (address1.AddressId == address2.AddressId)
                return true;
            else
                return false;
        }
    
        public int GetHashCode(Address address)
        {
            return address.AddressId.GetHashCode();
        }
    }
    

    My context.People.Add method call worked as expected after I made this change.

    If anyone knows why overriding the equality methods in the class causes
    EF 4.1 to only insert the first item in the collection, that would be
    great information.

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

Sidebar

Related Questions

Having Trouble with Entity Framework. I have been populating EntityReferences with an EntityKey inorder
Having trouble linking the Stomp.framework into an iPhone SDK application. http://code.google.com/p/stompframework/ I follow the
Im having trouble using a .NET COM in vb6, It compiles ok and I
I having trouble in dividing the HTML frames. I have been using the following
I am almost done with implementing a printing functionality, but I am having trouble
I am having trouble understanding / using name spaces with XML::LibXML package in Perl.
I'm having trouble figuring out the proper way to update nested data using Google
I'm having trouble with printing a rectangle to a shell using PaintListeners and GCs
I'm having some trouble printing out the contents of a linked list. I'm using
Having trouble with Delphi XE2 (update2) using MS SQL 2008 R2 (sp 2) or

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.