title: Composite Key/Id Mapping with NHibernate
body: <p>I have the following tables in my database:</p>
  
  <pre><code>Announcements:
  - AnnouncementID (PK)
  - Title
  
  AnouncementsRead (composite PK on AnnouncementID and UserID):
  - AnnouncementID (PK)
  - UserID (PK)
  - DateRead
  
  Users:
  - UserID (PK)
  - UserName
  </code></pre>
  
  <p>Usually I'd map the "AnnouncementsRead" using a many-to-many relationship but this table also has an additional "DateRead" field.</p>
  
  <p>So far I have defined the following entities:</p>
  
  <pre><code>    public class Announcement
      {
          public virtual int AnnouncementID { get; set; }
          public virtual string Title { get; set; }
          public virtual IList&lt;AnnouncementRead&gt; AnnouncementsRead { get; private set; }
  
          public Announcement()
          {
              AnnouncementsRead = new List&lt;AnnouncementRead&gt;();
          }
      }
  
      public class AnnouncementRead
      {
          public virtual Announcement Announcement { get; set; }
          public virtual User User { get; set; }
          public virtual DateTime DateRead { get; set; }
      }
  
      public class User
      {
          public virtual int UserID { get; set; }
          public virtual string UserName { get; set; }
          public virtual IList&lt;AnnouncementRead&gt; AnnouncementsRead { get; private set; }
  
          public User()
          {
              AnnouncementsRead = new List&lt;AnnouncementRead&gt;();
          }
   }
  </code></pre>
  
  <p>With the following mappings:</p>
  
  <pre><code>public class AnnouncementMap : ClassMap&lt;Announcement&gt;
  {
      public AnnouncementMap()
      {
          Table("Announcements");
          Id(x =&gt; x.AnnouncementID);
          Map(x =&gt; x.Title);
          HasMany(x =&gt; x.AnnouncementsRead)
              .Cascade.All();
      }
  }
  
  public class AnnouncementReadMap : ClassMap&lt;AnnouncementRead&gt;
  {
      public AnnouncementReadMap()
      {
          Table("AnnouncementsRead");
          CompositeId()
              .KeyReference(x =&gt; x.Announcement, "AnnouncementID")
              .KeyReference(x =&gt; x.User, "UserID");
          Map(x =&gt; x.DateRead);
      }
  }
  
  public class UserMap : ClassMap&lt;User&gt;
  {
      public UserMap()
      {
          Table("Users");
          Id(x =&gt; x.UserID);
          Map(x =&gt; x.UserName);
          HasMany(x =&gt; x.AnnouncementsRead)
              .Cascade.All();
      }
  }
  </code></pre>
  
  <p>However when I run this I receive the following error:</p>
  
  <pre><code>"composite-id class must override Equals(): Entities.AnnouncementRead"
  </code></pre>
  
  <p>I'd appreciate it if someone could point me in the right direction.  Thanks</p>