Mayur Dongre
Mayur Dongre

Reputation: 133

nhibernate foreign key relationship on between two natual key columns

I want to define foreign key relationship between two tables using natural keys... By default nhibernate is considering the other column as primary key...

Class A{
   public virtual int id {get; set;}
   public virtual int username {get; set;}
}

Class B{
   public virtual int id {get; set;}
   public virtual int username {get; set;}
}

I want to define foreign key relation from class B (col: username) on class A (col:username)

ManyToOne<ClassA>( x=> x.ClassA, map => { map.Column("username"); };

Nhibenate is creating it on username to id and not on username to username.. how can i achieve this?

Thanks, mayur

Upvotes: 0

Views: 290

Answers (1)

user2165424
user2165424

Reputation:

please have a look at the link: NHibernate - Mapping a String Foreign Key

you need to use the property-ref tag in key section and specify the name of the column which is unique in other table.

<bag name=”Inventory” table=”INVENTORY” lazy=”true”>
 <key column=”ID” property-ref=”OID” />
 <one-to-many class=”GuitarStore.Common.Inventory” />
 </bag>

Upvotes: 1

Related Questions