流畅的nHibernate映射问题

时间:2011-05-26 12:43:38

标签: .net nhibernate fluent-nhibernate

我有两张桌子,比如商店和部门。

ShopTable
  ShopId
  ShopName

DepartmentTable
  DepartmentId
  ShopId
  Name
  Type

好的,现在我将shoptable映射到Shop class

   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);
   }  

1 个答案:

答案 0 :(得分:2)

在问题中显示的类结构将不支持给定数据库表模式的操作。 Shop类与Department类之间存在一对多的关系。这是一个适用于您的表结构的映射:

  public class Shop
   {
        public virtual int ShopId {get; set;}
        public virtual string ShopName {get; set;}
        public virtual IList<Department> Departments { get; set; }
   }

   public class Department
   {
       public virtual int DepartmentId { get; set; }
       public virtual Shop Shop { 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);

       HasMany(c => c.Departments);
   }

   public class DepartmentMap : ClassMap<Department>
   {
       Id(x => x.DepartmentId);

       References(x => x.Shop, "ShopId");

       Map(x=> x.Name);
       Map(x=> x.Type);
   }