Note that there are some explanatory texts on larger screens.

plurals
  1. POIssues when trying to remove the connection between two objects in a many-to-many relationship
    primarykey
    data
    text
    <p>I have two entities: Group and ContactInfo. These are connected with a many-to-many relationship (mapped using Fluent NHibernate).</p> <p>I'm not able to remove a "ContactInfo" from a Group. The join-table is not updated. (It is when inserting new elements)</p> <p>My code (excerpt):</p> <pre><code>public class GroupMap : ClassMap&lt;Group&gt; { public GroupMap() { Id(x =&gt; x.GroupID); Map(x =&gt; x.GroupName).Not.Nullable(); References(x =&gt; x.Function); HasManyToMany(x =&gt; x.Contacts) .Inverse() .Cascade.All() // tried Cascade.AllDeleteOrphans() .Not.LazyLoad() .WithTableName("Contacts"); } } public class ConcactInfoMap : ClassMap&lt;ContactInfo&gt; { public ConcactInfoMap() { Id(x =&gt; x.ContactInfoID); References(x =&gt; x.ContactInfoType).Not.Nullable().Not.LazyLoad(); Map(x =&gt; x.ContactInfoValue).Not.Nullable(); References(x =&gt; x.Person) .Not.LazyLoad(); HasManyToMany(x =&gt; x.Groups) .Cascade.All() // tried Cascade.AllDeleteOrphans() .Not.LazyLoad() .WithTableName("Contacts"); } } // The helper method public virtual void RemoveFromGroup(Group group) { this.Groups.Remove(group); group.Contacts.Remove(this); } // The deletion contactInfo.RemoveFromGroup(group); m_ContactInfoRepository.Update(ci); </code></pre> <p>Any suggestions to what I'm doing wrong? Also, If anyone have any good resources on how to avoid lots of <code>.Not.LazyLoad()</code> , I would highly appreciate it :-)</p> <p><strong>Updates:</strong></p> <p>I also noticed that the two ID columns in the join-table is not set to be a primary key. I noticed this when I managed to insert equal rows into it. How do I avoid this? Is this a case where I should use <code>Set</code> over <code>Bag</code>? </p> <p>I've read up a little on Bag vs Set, and to me it seems like Set perhaps would solve my "unique row" issue, but still, as this is just a join table, my biggest concern is to be able to delete "connection", and remove a <code>ContactInfo</code> from a <code>Group</code>.</p> <p>I tried removing the Cascade statements as mentioned in the comments, but it didn't make any difference when it came to deleting.</p> <p><strong>Updates NEW</strong></p> <p>After doing some debugging I noticed that when I call <code>this.Groups.Remove(group)</code> nothing happens. What can the reason be? Or..I checked the hashcodes in the "Immediate window", and they are not equal. Have I done something wrong or do I just need to override equals()?</p> <p><strong>Solution</strong></p> <p>Overriding the Equals for Group and ContactInfo solved it! Are there any rules saying that I should always override it for NHibernate entities?</p>
    singulars
    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.
 

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