Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fluent Nhibernate HasMany on non primary key

**Table Order**
Id (PK)
NonUniqueId

**Table OrderLine**
Id (PK)
OrderNonUniqueId
Text

I have a legacy database where there OrderLine references Order via non primary key. A Order Line may belong to many Orders.

How can this be mapped at HasMany?

**OrderMap**
HasMany(x => x.OrderLines)
      .KeyColumn("OrderNonUniqueId")

(will not work since it uses the primary key Order.Id)

like image 702
Stig Avatar asked May 08 '13 09:05

Stig


1 Answers

Have you tried using PropertyRef?

public OrderMap()
{
  ...
  Map(x => x.NonUniqueId);
  HasMany<OrderLine>(x => x.Lines)
    .KeyColumn("OrderNonUniqueId")
    .PropertyRef("NonUniqueId");
  ...
}

It seems the extra Map is necessary otherwise fluent nhibernate complains. If you map the one-to-many within an hbm.xml file the extra property mapping for the NonUniqueId isn't required.

like image 64
mickfold Avatar answered Oct 22 '22 05:10

mickfold