title: NHibernate Many-to-many with Count
body: <p>I have entities <code>Post</code> and <code>Tag</code>, where there is a many-to-many relationship between the two (e.g. each post can have one or more tags, and each tag can be associated with any number of posts).</p>
  
  <p>What I would like to achieve is to have the <code>Tag</code> entity to have a field providing the number of posts which are associated with that tag. However, I don't know how to go about getting this without getting the entire collection of <code>Post</code>s (which I'd like to avoid).</p>
  
  <p>I am using Fluent NHibernate and my entities and mappings look like this currently:</p>
  
  <p><strong>Entities/Post.cs</strong></p>
  
  <pre><code>public class Post : PersistentBase
  {
      public virtual string Title { get; set; }
      /* snip */
  
      private IList&lt;Tag&gt; tags = new List&lt;Tag&gt;();
      public virtual IEnumerable&lt;Tag&gt; Tags {
          get { return tags; }
      }
  
      public virtual void AddTag(Tag tag) {
          this.tags.Add(tag);
      }
  }
  </code></pre>
  
  <p><strong>Mappings/PostMap.cs</strong></p>
  
  <pre><code>public class PostMap : ClassMap&lt;Post&gt;
  {
      public PostMap()
      {
          Id(x =&gt; x.Id).GeneratedBy.HiLo("99");
          Map(x =&gt; x.Title);
          /* snip */
          HasManyToMany(x =&gt; x.Tags);
      }
  }
  </code></pre>
  
  <p><strong>Entities/Tag.cs</strong></p>
  
  <pre><code>public class Tag : PersistentBase
  {
      public virtual string Name { get; set; }
  
      public static Tag Create(string name) {
          return new Tag { Name = name };
      }
  }
  </code></pre>
  
  <p><strong>Mappings/TagMap.cs</strong></p>
  
  <pre><code>public class TagMap : ClassMap&lt;Tag&gt;
  {
      public TagMap ()
      {
          Id(x =&gt; x.Id).GeneratedBy.HiLo("99");
          Map(x =&gt; x.Name).Unique();
      }
  }
  </code></pre>
  
  <p>Ideally what I'd like to achieve is to be able to add into Entities/Tag.cs, something like the following:</p>
  
  <pre><code>public virtual int PostCount { get; set; }
  </code></pre>
  
  <p>And then have that pre-filled with the number of posts using that tag.</p>
  
  <p>How might I go about doing this? (is it indeed possible?)</p>