在我的数据库中,有“工作分配”表和“工作场所”表,从概念上讲,每个工作分配都恰好在一个工作场所进行。分配表包含一列Workplace,这是引用Workplace表的WorkplaceName列的外键。

这是模式:

table Workplace (
  ID int primary key,
  WorkplaceName int
)

table Assignment (
  Workplace int
)

和我的映射:
class Workplace
{
  public virtual int ID { get; set; }
  public virtual int WorkplaceName { get; set; }
}

class Assignment
{
  public virtual Workplace { get; set; }
}

class AssignmentMap : ClassMap<Assignment>
{
  public AssignmentMap()
  {
    References(a => a.Workplace);
  }
}

在运行时,我得到了异常



这里的问题似乎是FH在表的主键Workplace.ID列中寻找Assignment.Workplace属性的值。正确的位置应该是Workplace.WorkplaceName列。我试图使用ForeignName方法,以为我可以用这种方式指定WorkplaceName。如何告诉FH如何加入正确的列?

最佳答案

我在这里找到了答案:FluentNHibernate Many-To-One References where Foreign Key is not to Primary Key and column names are different

解决的办法是使用PropertyRef;该行现在显示为:

References(x => x.Workplace).PropertyRef(x => x.WorkplaceName).Fetch.Join();

我没有意识到的是,我需要引用客户端对象的属性WorkplaceName,而不是尝试指示映射使用服务器端列WorkplaceName。

10-08 13:33