I’m using Fluent NHibernate to map my entities and I’ve come to a set that I’m having probelsm gettting mapped. I’ve tried different variations of SubclassMap but I can’t seem to get Cart, Project or Order mapped correctly. Item, CartItem, OrderItem and ProjectItem map fine. I’m fairly new to NHibernate and I just can’t figure out how to tell it to do what I want.
EDIT: This is the schema I’m going for with this inheritance structure:
public abstract class Item
{
public virtual int Id {get;set;}
public virtual decimal Price {get;set;}
}
public class CartItem : Item
{
public virtual string ProjectName {get;set;}
}
public class OrderItem : Item
{
public virtual IList<Payment> Payments {get;set;}
}
public class ProjectItem : Item
{
public virtual string ProjectName {get;set;}
}
public abstract class ItemCollection<T> where T: Item
{
public virutal IList<T> Items {get;set;}
}
public abstract class CommerceCollection<T> : ItemCollection<T> where T : Item
{
public virtual decimal Total {get;set;}
}
public class Project : ItemCollection<ProjectItem>
{
public virtual string ProjectName {get;set;}
}
public class Cart : CommerceCollection<CartItem> {}
public class Order : CommerceCollection<OrderItem>
{
public virtual string OrderTrackingNumber {get;set;}
}
/*------- MAPS -------*/
public class ItemMap: ClassMap<Item>
{
public ItemMap()
{
MapId();
Map(x=> x.Price);
}
}
public class OrderItemMap : SubclassMap<OrderItem>
{
public OrderItemMap()
{
Map(x => x.OrderItemProperty);
}
}
public class CartItemMap : SubclassMap<CartItem>
{
public CartItemMap()
{
Map(x => x.CartItemProperty);
}
}
public class ProjectItemMap : SubclassMap<ProjectItem>
{
public ProjectItemMap()
{
Map(x => x.ProjectItemProperty);
}
}
public class CartMap : ClassMap<Cart>
{
public CartMap()
{
Map(x => x.Total);
HasMany(x => x.Items);
}
}
public class OrderMap : ClassMap<Order>
{
public OrderMap()
{
Map(x => x.Total);
HasMany(x => x.Items);
}
}
I figured out what I needed to do. For the classes that implemented the generic base class (CommerceCollection) I had to add a map for the generic collection with it’s argument and then specify the KeyColumn property on the subclass.
Here is the new CartMap:
Nhibernate was generating SQL like below which would fail on the ‘ in the column name.