2
我有兩個表,可以說店和部門。流利的nHibernate映射問題
ShopTable
ShopId
ShopName
DepartmentTable
DepartmentId
ShopId
Name
Type
好了,現在我映射shoptable到店鋪等級
public class Shop
{
public virtual int ShopId {get; set;}
public virtual string ShopName {get; set;}
public virtual Department Toys { get; set; }
public virtual Department Hardware { get; set; }
public virtual Department Food { get; set; }
}
public class Department
{
public virtual int DepartmentId { get; set; }
public virtual int ShopId { get; set; }
public virtual string Name { get; set; }
public virtual DepartmentType Type { get; set; }
}
如何映射玩具硬件和食品?
public class ShopMap : ClassMap<Shop>
{
Id(x => x.ShopId);
Map(x=> x.ShopName);
// How do I map these according to the DepartmentType enum?
Map(x=> x.Toys);
Map(x=> x.Hardware);
Map(x=> x.Food);
}
public class DepartmentMap : ClassMap<Department>
{
Id(x => x.DepartmentId);
Map(x=> x.ShopId);
Map(x=> x.Name);
Map(x=> x.Type);
}
是啊,我知道,我可以有對象的名單,但你說,它不是」是否有可能以任何方式將它們作爲單獨的對象? 會幫助我很多,因爲如果他們在列表中,我將不得不循環每次訪問其中的一個列表。 – TuomasK 2011-05-26 19:45:28
您無法避免搜索您想要的特定部門的館藏。推薦的類將允許NHibernate爲您的表結構生成正確的SQL。你可以在你的類的屬性或方法上封裝集合上的特定查詢。這些方法或屬性不會在NHibernate中映射,因此它們不會影響數據庫操作。例如:public department toys {get {Departments.FirstOrDefault(d => d.DepartmentId == ToysDepartmentId); }} – 2011-05-26 21:11:41
這有點奇怪,因爲使用集合你可以這樣做。 HasMany(x => x.Departments).KeyColumn(「ShopId」)。其中(y => y.Type == DepartmentType.Toys); – TuomasK 2011-05-27 05:36:59