Suppose I have the following two classes:
public class User : Entity
{
public virtual IList<Item> Items { get; set; }
}
public class Item : Entity
{
public virtual User Owner { get; set; }
}
I created two mapping classes:
public class UserMap : ClassMap<User>
{
public UserMap()
{
Id(x => x.Id);
HasMany(x => x.Items);
}
}
public class ItemMap : ClassMap<Item>
{
public ItemMap()
{
Id(x => x.Id);
References(x => x.Owner);
}
}
This will result in a table Itemthat has a column UserIdand a column OwnerId. When I use KeyColumn("OwnerId")for matching HasMany, it only works with the column OwnerId, but I would like to avoid it. Is there a way to tell NHibernate to use the column created by the mapping in ItemMap?
Why I donβt want to explicitly specify a column:
The column name is OwnerIdautomatically generated based on the property name and some rules. If I change either the rules or the property name, I need to remember to change this KeyColumn. So basically this is not save refactoring.