..with the KeyColumn method or change the default behaviour with a Convention. "
That quote is from the fluent nhibernate mapping guide. I can't understand why it does this instead of, for that example, using the correct (column) name for the Author class primary key.
I know one solution, which is to use .KeyColumn like it is here in my own mappings (my question is coming after this code):
public class SupplierMap : ClassMap<Supplier>
{
public SupplierMap()
{
Table("Suppliers");
LazyLoad();
Id(x => x.SupplierID, "SupplierID");
Map(x => x.CompanyName).Not.Nullable();
Map(x => x.ContactName);
Map(x => x.ContactTitle);
Map(x => x.Address);
Map(x => x.City);
Map(x => x.Region);
Map(x => x.PostalCode);
Map(x => x.Country);
Map(x => x.Phone);
Map(x => x.Fax);
Map(x => x.HomePage);
HasMany(x => x.Products).KeyColumn("ProductID").Inverse().Cascade.All();
}
}
And the Product mapping is:
public class ProductMap : ClassMap<Product>
{
public ProductMap()
{
Table("Products");
LazyLoad();
Id(x => x.ProductID);
References(x => x.Supplier, "SupplierID");
Map(x => x.ProductName).Not.Nullable();
Map(x => x.QuantityPerUnit);
Map(x => x.UnitPrice);
Map(x => x.UnitsInStock);
Map(x => x.UnitsOnOrder);
Map(x => x.ReorderLevel);
Map(x => x.Discontinued).Not.Nullable();
}
}
So my question is, is there a more elegant way? I was quite disappointed to see two columns on my product table for id (ProductID and Product_ID). As mentioned this was fixed with KeyColumn("ProductID") as above. Is there a way I can set this in the Product mapping, or even in the configuration? I don't think I understand the "override the default behaviour with a convention" -- that is probably where the answer is.