FluentNHibernate Many-to-One Links in which the foreign key is not the first key and the column names are different

I've been sitting here for an hour trying to figure this out ...

I have 2 tables (abbreviated):

CREATE TABLE TRUST ( TRUSTID NUMBER NOT NULL, ACCTNBR VARCHAR(25) NOT NULL ) CONSTRAINT TRUST_PK PRIMARY KEY (TRUSTID) CREATE TABLE ACCOUNTHISTORY ( ID NUMBER NOT NULL, ACCOUNTNUMBER VARCHAR(25) NOT NULL, TRANSAMT NUMBER(38,2) NOT NULL POSTINGDATE DATE NOT NULL ) CONSTRAINT ACCOUNTHISTORY_PK PRIMARY KEY (ID) 

I have 2 classes that essentially reflect them:

 public class Trust { public virtual int Id {get; set;} public virtual string AccountNumber { get; set; } } public class AccountHistory { public virtual int Id { get; set; } public virtual Trust Trust {get; set;} public virtual DateTime PostingDate { get; set; } public virtual decimal IncomeAmount { get; set; } 

}

How do I do many-to-one mapping in FluentNHibernate to get an AccountStore for Trust? In particular, since it is associated with a different column than the Trust TRUSTID primary key, and the column to which it refers is also called differently (ACCTNBR vs. ACCOUNTNUMBER) ???? Here is what I still have - how do I link to AccountHistoryMap for trust?

 public class TrustMap : ClassMap<Trust> { public TrustMap() { Table("TRUST"); Id(x => x.Id).Column("TRUSTID"); Map(x => x.AccountNumber).Column("ACCTNBR"); } } public class AccountHistoryMap : ClassMap<AccountHistory> { public AccountHistoryMap() { Table("TRUSTACCTGHISTORY"); Id (x=>x.Id).Column("ID"); References<Trust>(x => x.Trust).Column("ACCOUNTNUMBER").ForeignKey("ACCTNBR").Fetch.Join(); Map(x => x.PostingDate).Column("POSTINGDATE"); ); 

I tried several different variants of the above line, but I can’t get anything to work - it discards the AccountHistory and proxy data for Trust; however, it does not specify a trust string with the given identifier.

It should be something simple. Is anyone

Thanks in advance.

+7
orm nhibernate fluent-nhibernate
source share
1 answer

You need to use the ref property :

 public class AccountHistoryMap : ClassMap<AccountHistory> { public AccountHistoryMap() { Table("TRUSTACCTGHISTORY"); Id (x=>x.Id).Column("ID"); References(x => x.Trust, "ACCOUNTNUMBER").PropertyRef("ACCTNBR").Fetch.Join(); Map(x => x.PostingDate).Column("POSTINGDATE"); } } 
+14
source share

All Articles