具有额外列的流畅 Nhibernate 多对多映射

发布于 2024-08-25 07:19:47 字数 765 浏览 2 评论 0原文

我想使用流畅的 Nhibernate 来映射这样的东西,但我不确定如何映射库存表

这是我拥有的表:

产品(ID、名称、...)

仓库(ID、名称、...)

库存(Product_id、Warehouse_id、StockInHand)

和如下所示的映射

 Public ProductMap()
 {
     Id(x => x.Id);
     Map(x => x.Name);
  
     HasManyToMany(x => x.StoresStockedIn)
       .Cascade.All()
      .Inverse()
      .Table("Inventory");
 }

 public WarehouseMap()
 {
     Id(x => x.Id);
     Map(x => x.Name);      
     HasManyToMany(x => x.Products)
     .Cascade.All()
     .Table("Inventory");
 }

我面临的问题是如何映射StockInHand(库存模型应该如何映射?)。

或者还有其他方法来模拟这种情况吗?

我已经阅读了一些现有问题,但尚未清楚地了解该怎么做。

谢谢

I want to map sth like this using fluent Nhibernate but I am not sure how to map the inventory table

This is the tables I have :

Product (Id,Name, ...)

Warehouse(Id, Name, ...)

Inventory(Product_id, Warehouse_id, StockInHand)

and Mappings like below

 Public ProductMap()
 {
     Id(x => x.Id);
     Map(x => x.Name);
  
     HasManyToMany(x => x.StoresStockedIn)
       .Cascade.All()
      .Inverse()
      .Table("Inventory");
 }

 public WarehouseMap()
 {
     Id(x => x.Id);
     Map(x => x.Name);      
     HasManyToMany(x => x.Products)
     .Cascade.All()
     .Table("Inventory");
 }

The problem I face is that how can I map the StockInHand (how should the inventory model mapping?).

or are there other way to model this scenario ?

I have read some existing questions but not yet get clear understand what to do.

Thanks

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

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

发布评论

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

评论(1

羁绊已千年 2024-09-01 07:19:47

就 NHibernate 而言,你们的关系不是多对多。真正的多对多没有额外的列,例如示例中的 StockInHand。

您必须将其映射为两个一对多关系,并将 Inventory 映射为实体。

就像(我跳过了其他属性):

public class Product
{
  public List<Inventory> Inventory { get; set; }
}

public class Warehouse
{
  public List<Inventory> Inventory { get; set; }
}

public class Inventory
{
  public Product Product { get; set; }
  public Warehouse Warehouse { get; set; }
  public bool StockInHand { get; set; }
}

public ProductMap() {
    Id(x => x.Id);
    Map(x => x.Name);
    HasMany(x => x.Inventory)
     .Cascade.All()
     .Inverse()
     .Table("Inventory");
}

public WarehouseMap()
{
    Id(x => x.Id);
    Map(x => x.Name);      
    HasMany(x => x.Inventory)
     .Cascade.All()
     .Inverse()
     .Table("Inventory");
}

public InventoryMap()
{
    CompositeId()
      .KeyReference(x => x.Product, "Product_id")
      .KeyReference(x => x.Warehouse, "Warehouse_id")

    Map(x => x.StockInHand);
}

Your relationship is not a many-to-many as far as NHibernate is concerned. A true many-to-many has no additional columns, such as StockInHand in your example.

You have to map this as two one-to-many relationships, and map Inventory as an entity.

Something like (i've skipped the other properties):

public class Product
{
  public List<Inventory> Inventory { get; set; }
}

public class Warehouse
{
  public List<Inventory> Inventory { get; set; }
}

public class Inventory
{
  public Product Product { get; set; }
  public Warehouse Warehouse { get; set; }
  public bool StockInHand { get; set; }
}

public ProductMap() {
    Id(x => x.Id);
    Map(x => x.Name);
    HasMany(x => x.Inventory)
     .Cascade.All()
     .Inverse()
     .Table("Inventory");
}

public WarehouseMap()
{
    Id(x => x.Id);
    Map(x => x.Name);      
    HasMany(x => x.Inventory)
     .Cascade.All()
     .Inverse()
     .Table("Inventory");
}

public InventoryMap()
{
    CompositeId()
      .KeyReference(x => x.Product, "Product_id")
      .KeyReference(x => x.Warehouse, "Warehouse_id")

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