FluentNHibernate - AutoMappings生成不正确的一对多列密钥

时间:2009-10-20 14:44:15

标签: nhibernate fluent-nhibernate nhibernate-mapping

我是NHibernate和FNH的新手,我正在尝试使用FluentNHibernate AutoMappings功能映射这些简单的类:

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);
    }
}

我也使用了几个约定:

 public class MyForeignKeyConvention : IReferenceConvention
{

    #region IConvention<IManyToOneInspector,IManyToOneInstance> Members

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

    #endregion

}

问题是FNH正在为Seasons属性映射生成以下部分:

<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>

上面的列名应该是fk_TVShow而不是TVShow_Id。如果修改FNH生成的hbm文件,则代码可以正常工作。

有谁知道这是错的吗?

提前致谢。

1 个答案:

答案 0 :(得分:0)

您是否已通过调试器中的自动映射以确保调用您的约定? 假设您正确连接了它,您可能需要实现ReferenceConvention的Accept接口