views:

47

answers:

1

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.

A: 

Have you stepped through the auto map in the debugger to make sure your convention is being called?
Assuming you have it wired up correctly you may need to implement the Accept interface for ReferenceConvention

Aaron Fischer
Yeah, the convention is being called. Besides, on the Season class mappings I can see the mapping below:<many-to-one name="TVShow"> <column name="fk_TVShow" /> </many-to-one>
Alberto