Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p>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???</p> <p>Here are the original equality methods which caused the issue:</p> <pre><code>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(); } </code></pre> <p>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.</p> <pre><code>public class AddressEqualityComparer : IEqualityComparer&lt;Address&gt; { 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(); } } </code></pre> <p>My context.People.Add method call worked as expected after I made this change. </p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload