title: Fluent Nhibernate ManyToMany collection - not saving associations
body: <p>I can't get my head around why this isn't working..</p>
  
  <p>I have a relatively clean entity model consisting of POCOs created with DDD in mind (while probably not following most rules even loosely).</p>
  
  <p>I am using Fluent NHibernate to do the mapping. I am also using SchemaExport to create the database schema, with minimum input from me on how to do it. NHibernate is free to choose the best way.</p>
  
  <p>I have two entities with Many-to-many relationships with each other (non-interesting code removed); MediaItem and Tag; MediaItems can have many tags, Tags can be applied to many MediaItems, and I want collections on both sides so I can easily get at stuff.</p>
  
  <p>(A bit of a formatting issue below, sorry)</p>
  
  <ul>
  <li><p>MediaItem:</p>
  
  <pre><code>public class MediaItem
  {
  
  private IList&lt;Tag&gt; _tags;
  
  public virtual long Id { get; set; }
  
  public virtual string Title { get; set; }
  
  public virtual IEnumerable&lt;Tag&gt; Tags { get { return _tags; } }
  
  public MediaItem()
  {
      _tags = new List&lt;Tag&gt;();
  }
  
  public virtual void AddTag(Tag newTag)
  {
      _tags.Add(newTag);
      newTag.AddMediaItem(this);
  }
  </code></pre>
  
  <p>}</p></li>
  <li><p>Tag:</p>
  
  <p>public class Tag
  {</p>
  
  <pre><code>private IList&lt;MediaItem&gt; _mediaItems;
  public virtual long Id { get; set; }
  public virtual string TagName { get; set; }
  public virtual IEnumerable&lt;MediaItem&gt; MediaItems { get { return _mediaItems; } }
  
  public Tag()
  {
      _mediaItems = new List&lt;MediaItem&gt;();
  }
  
  protected internal virtual void AddMediaItem(MediaItem newItem)
  {
      _mediaItems.Add(newItem);
  }
  </code></pre>
  
  <p>}</p></li>
  </ul>
  
  <p>I have tried to be smart about only exposing the collections as IEnumerable, and only allowing adding items through the methods. I also hear that only one side of the relationship should be responsible for this - thus the contrived AddMediaItem() on Tag.</p>
  
  <p>The MediaItemMap looks like this:</p>
  
  <pre><code>public class MediaItemMap : ClassMap&lt;MediaItem&gt;
  {
      public MediaItemMap()
      {
          Table("MediaItem");
  
          Id(mi =&gt; mi.Id);
  
          Map(mi =&gt; mi.Title);
  
          HasManyToMany&lt;Tag&gt;(mi =&gt; mi.Tags)
              .Access.CamelCaseField(Prefix.Underscore)
              .Cascade.SaveUpdate();
      }
  }
  </code></pre>
  
  <p>The Tag mapping looks like this:</p>
  
  <pre><code>public class TagMap : ClassMap&lt;Tag&gt;
  {
      public TagMap()
      {
          Table("Tag");
  
          Id(t =&gt; t.Id);
  
          Map(t =&gt; t.TagName);
  
          HasManyToMany&lt;MediaItem&gt;(mi =&gt; mi.MediaItems)
              .Access.CamelCaseField(Prefix.Underscore)
              .Inverse();
      }
  }
  </code></pre>
  
  <p>Now I have some test code that drops the database schema, recreates it (since I am shotgun debugging my brains out here), and then runs the following simple code:</p>
  
  <pre><code>Tag t = new Tag { TagName = "TestTag" };
  MediaItem mi = new MediaItem { Title = "TestMediaItem" };
  
  mi.AddTag(t);
  
  var session = _sessionFactory.OpenSession();
  
  session.Save(mi);
  </code></pre>
  
  <p>Yep, this is test code, it will never live past the problem in this post.</p>
  
  <p>The MediaItem is saved, and so is the Tag. However, the association between them is not. NHibernate does create the association table "MediaItemsToTags", but it doesn't attempt to insert anything into it.</p>
  
  <p>When creating the ISessionFactory, I specify ShowSQL() - so I can see all the DDL sent to the SQL server. I can see the insert statement for both the MediaItem and the Tag tables, but there is no insert for MediaItemsToTags.</p>
  
  <p>I have experimented with many different versions of this, but I can't seem to crack it. Cascading is one possible problem, I've tried with Cascade.All() on both sides, Inverse() on both sides etc., but no dice.</p>
  
  <p>Can anyone tell me what is the correct way to map this to get NHibernate to actually store the association whenever I store my MediaItem?</p>
  
  <p>Thanks!</p>