Fluent Nhibernate Many-to-Many mapping with extra column

僤鯓⒐⒋嵵緔 提交于 2019-11-28 05:58:59

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);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!