I am trying to figure out how to get NHibernate to work with 1 to many and many to 1 type relationships. I have the following setup, however it doesn't seem to work
public class Product {
public Guid Id {get;set;}
public string Name {get;set;}
public string SKU {get;set;}
public decimal MSRP {get;set;}
}
public class Order {
public Guid Id {get;set;}
public DateTime CreatedOn {get;set;}
public IList<OrderLine> Lines {get;set;}
}
public class OrderLine {
public Guid Id {get;set;}
public Product Product {get;set;}
public int Qty {get;set;}
public decimal Price {get;set;}
}
public class ProductMap : ClassMap<Product>
{
Id(x => x.Id);
Map(x => x.Name);
Map(x => x.SKU);
Map(x => x.MSRP);
}
public class OrderMap : ClassMap<Order>
{
Id(x => x.Id);
Map(x => x.CreatedOn);
// This doesn't seem to work as I get an error that OrderLine is not mapped
HasMany(x => x.Lines);
}
public class OrderLineMap : ClassMap<OrderLine>
{
Id(x => x.Id);
Map(x => x.Qty);
Map(x => x.Price);
// This doesn't seem to work as I get an error the Product isn't mapped
// An association from the table OrderLine refers to an unmapped class: Product
References(x => x.Product);
}
public static class NHibernateUtils
{
public static ISessionFactory CreateSessionFactory(IPersistenceConfigurer persistenceConfigurer)
{
return Fluently.Configure()
.Database(persistenceConfigurer)
.Mappings(m =>
{
m.FluentMappings.Add<ProductMap>();
m.FluentMappings.Add<OrderLineMap>();
m.FluentMappings.Add<OrderMap>();
})
.ExposeConfiguration(c => new SchemaExport(c).Create(false, true))
.BuildConfiguration()
.BuildSessionFactory();
}
}
So I am curious -- what am I doing wrong. Note: I do NOT want to add the inverse properties to my objects. So OrderLine should NOT need a reference to Order and Product should NOT need a reference to a collection of Orders. Am I hitting a limitation? Or just missing something simple?
Ok, I figured out the main issue -- I was missing the PropertyRef on the References() call. Looks like this is required? If so, should the exception be a bit more specific?