Assume 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 Item that has a column UserId and a column OwnerId. When I use KeyColumn("OwnerId") on the HasMany mapping, it works with only the OwnerId column, but I would like to avoid that. Is there a way to tell NHibernate, to use the column created by the mapping in ItemMap?
Why I want to avoid specifying the column explicitly:
The column name OwnerId is automatically being generated based on the name of the property and some rules. If I change either the rules or the property name, I need to remember to change that KeyColumn, too. So, basically, it is not refactoring save.
Updated: fixed bug
if you apply the rules in the maps then
if you apply the rules as conventions then you need to implement
IHasManyConventionand apply the same rules on theEntityTypeand propertyname (which you have to get through reflection from the ChildType)Update: