0

Mapping a 1:n relation automatically creates a FK column, in below example, a column Author_id in the Book table. But how do I make this value available as a property of the Book entity? If I create a property int AuthorID, is it possible to make NHibernate use this property as the foreign key, so I can access it programatically?

public class Author
{
  public IList<Book> Books { get; set; }
}

//AuthorMap:
HasMany(x => x.Books);
bretddog
  • 5,411
  • 11
  • 63
  • 111

1 Answers1

0

As I understand from the question you need a bi-derectional relationship. Just add property Author in the Book class

public virtual Author Author {get; set;}

And map it as reference in Book Mappping file

Reference(x => x.Author);

You could specify the same FK column name for both mappings, to ensure that you link same table column.

In case you really need to have int AuthorID in book, that create this property and set it as Not.Update and Not.Insert, just make it readonly.

isuruceanu
  • 1,157
  • 5
  • 23
  • No I did not want bi-directional. Just wonder if/how it's possible to access the FK of a uni-directional relation as a property. – bretddog Nov 03 '11 at 21:26