Search code examples
c#nhibernatefluent-nhibernatefluent-nhibernate-mapping

fluentNhibernate: how to add an extra attribute to a relationship (aka relationship attribute)


I have two entities, Albums and Photos, with a many-to-many relationship between then. Its all working fine. What I want is to add an relationship attribute i.e. an extra attribute to the mapping besides album_id and photo_id, for example the datetime the photo was added or an attribute that says if the photo is visible or not on that album.

public class Photo
{   public virtual int Id { get; set; }
    public virtual string Title { get; set; }
    public virtual ICollection<Album> Albums { get; set; }
}

public class Album
{
public virtual int Id {get;set;}
    public virtual string Title {get;set;}
    public virtual ICollection<Photo> Photos { get; set; }
}

The current mapping goes like this:

public class PhotosMap : ClassMap<Photo>
{
   public PhotosMap()
      {
         Id(x => x.Id);
         Map(x => x.Title); 
         HasManyToMany<Album>(x => x.Albums).Not.LazyLoad().Table("album_photos");               
         Table("photos");
      }
}

public class AlbumsMap : ClassMap<Album>
{
   public AlbumsMap()
     {
         Id(x => x.Id);
         Map(x => x.Title);                
         HasManyToMany<Photo>(x => x.Photos).Inverse().Not.LazyLoad().Table("album_photos");
         Table("album");
     }
 }

Solution

  • You can't. You need to explicitly model and map that mapping table now.