FluentNHibernate - AutoMappings producing incorrect one-to-many column key

Posted by Alberto on Stack Overflow See other posts from Stack Overflow or by Alberto
Published on 2009-10-20T14:44:14Z Indexed on 2010/03/30 18:03 UTC
Read the original article Hit count: 256

Hi

I'm new to NHibernate and FNH and am trying to map these simple classes by using FluentNHibernate AutoMappings feature:

public class TVShow : Entity
{
    public virtual string Title { get; set;}
    public virtual ICollection<Season> Seasons { get; protected set; }


    public TVShow()
    {
        Seasons = new HashedSet<Season>();
    }

    public virtual void AddSeason(Season season)
    {
        season.TVShow = this;
        Seasons.Add(season);
    }

    public virtual void RemoveSeason(Season season)
    {
        if (!Seasons.Contains(season))
        {
            throw new InvalidOperationException("This TV Show does not contain the given season");
        }
        season.TVShow = null;
        Seasons.Remove(season);
    }
}
public class Season : Entity
{
    public virtual TVShow TVShow { get; set; }
    public virtual int Number { get; set; }
    public virtual IList<Episode> Episodes { get; set; }

    public Season()
    {
        Episodes = new List<Episode>();
    }

    public virtual void AddEpisode(Episode episode)
    {
        episode.Season = this;
        Episodes.Add(episode);
    }

    public virtual void RemoveEpisode(Episode episode)
    {
        if (!Episodes.Contains(episode))
        {
            throw new InvalidOperationException("Episode not found on this season");
        }
        episode.Season = null;
        Episodes.Remove(episode);
    }
}

I'm also using a couple of conventions:

 public class MyForeignKeyConvention : IReferenceConvention
{

    #region IConvention<IManyToOneInspector,IManyToOneInstance> Members

    public void Apply(FluentNHibernate.Conventions.Instances.IManyToOneInstance instance)
    {
        instance.Column("fk_" + instance.Property.Name);
    }

    #endregion

}

The problem is that FNH is generating the section below for the Seasons property mapping:

<bag name="Seasons">
  <key>
    <column name="TVShow_Id" />
  </key>
  <one-to-many class="TVShowsManager.Domain.Season, TVShowsManager.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</bag>

The column name above should be fk_TVShow rather than TVShow_Id. If amend the hbm files produced by FNH then the code works.

Does anyone know what it's wrong?

Thanks in advance.

© Stack Overflow or respective owner

Related posts about fluent-nhibernate

Related posts about nhibernate