Fluent nHibernate 映射问题

发布于 2024-11-10 10:55:00 字数 1236 浏览 1 评论 0原文

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

ShopTable
  ShopId
  ShopName

DepartmentTable
  DepartmentId
  ShopId
  Name
  Type

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

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

I have two tables, lets say shop and department.

ShopTable
  ShopId
  ShopName

DepartmentTable
  DepartmentId
  ShopId
  Name
  Type

Ok now I map shoptable to 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; }
   }

How do I map the toys hardware and food?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

窝囊感情。 2024-11-17 10:55:00

给定数据库表架构,您在问题中显示的类结构将不支持您想要执行的操作。 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);
   }  

The class structure you show in the question won't support what you want to do given the database table schema. There is a one-to-many relationship from the Shop class to the Department class. Here is a mapping that will work given your table structure:

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

For a mapping of:

   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);
   }  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文