Fluent nHibernate - 如何在连接表上映射非键列?

发布于 2024-09-05 01:28:45 字数 768 浏览 3 评论 0原文

以 Fluent nHibernate 网站上提供的示例为例,我需要稍微扩展一下:

alt text
(来源: Fluentnhibernate.org

我需要向 StoreProduct 表添加“数量”列。我如何使用 nHibernate 映射它?

为上面给定的场景提供了示例映射,但我不确定如何将 Quantity 列映射到 Product 类上的属性:

public class StoreMap : ClassMap<Store>
{
  public StoreMap()
  {
    Id(x => x.Id);
    Map(x => x.Name);
    HasMany(x => x.Employee)
      .Inverse()
      .Cascade.All();
    HasManyToMany(x => x.Products)
     .Cascade.All()
     .Table("StoreProduct");
  }
}

Taking an example that is provided on the Fluent nHibernate website, I need to extend it slightly:

alt text
(source: fluentnhibernate.org)

I need to add a 'Quantity' column to the StoreProduct table. How would I map this using nHibernate?

An example mapping is provided for the given scenario above, but I'm not sure how I would get the Quantity column to map to a property on the Product class:

public class StoreMap : ClassMap<Store>
{
  public StoreMap()
  {
    Id(x => x.Id);
    Map(x => x.Name);
    HasMany(x => x.Employee)
      .Inverse()
      .Cascade.All();
    HasManyToMany(x => x.Products)
     .Cascade.All()
     .Table("StoreProduct");
  }
}

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

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

发布评论

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

评论(1

め七分饶幸 2024-09-12 01:28:45

一种建议是不使用 hasManyToMany 映射,并为 StoreProduct(Product 的子类)使用单独的映射类。

新的商店映射

public class StoreMap : ClassMap<Store>
{
  public StoreMap()
  {
    Id(x => x.Id);
    Map(x => x.Name);
    HasMany(x => x.Employee)
      .Inverse()
      .Cascade.All();
    HasMany(x => x.Products)
     .Cascade.All();
  }
}

NB 将 HasManyToMany 更改为 HasMany。

商店产品的新子类映射

public class StoreProductMap : SubclassMap<StoreProduct>
{
   References(x=>x.Store);

   Map(x=>x.Quantity);
}

新的 StoreProduct 实体

public class StoreProduct : Product
{
    public virtual Store Store {get;set;}
    public virtual int Quantity {get;set;}
}

希望有所帮助。

One suggestion would be to not use the hasManyToMany mapping and have a separate mapping class for StoreProduct which is a subclass of Product.

New Store Mapping

public class StoreMap : ClassMap<Store>
{
  public StoreMap()
  {
    Id(x => x.Id);
    Map(x => x.Name);
    HasMany(x => x.Employee)
      .Inverse()
      .Cascade.All();
    HasMany(x => x.Products)
     .Cascade.All();
  }
}

NB changed HasManyToMany to HasMany instead.

New sub class mapping for Store Product

public class StoreProductMap : SubclassMap<StoreProduct>
{
   References(x=>x.Store);

   Map(x=>x.Quantity);
}

New StoreProduct entity

public class StoreProduct : Product
{
    public virtual Store Store {get;set;}
    public virtual int Quantity {get;set;}
}

Hope that helps.

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