我使用流利NHibernate映射我的實體,我來到一個集,我有probelsm gettting映射。我已經嘗試了SubclassMap的不同變體,但我似乎無法正確映射Cart,Project或Order。 Item,CartItem,OrderItem和ProjectItem很好地映射。我對NHibernate相當陌生,我只是無法弄清楚如何告訴它做我想做的事。與泛型基類的NHibernate映射
編輯:這是我與這個繼承結構去爲架構:
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);
}
}
似乎Cart,Project和Order不是從Item繼承的。這是對的嗎? – 2011-05-11 18:37:07
這是正確的,購物車項目和訂單都有收藏項目 – scottm 2011-05-12 16:49:24