Fluent Nhibernate Many-to-Many mapping with extra column

本秂侑毒 提交于 2019-11-27 05:37:15

问题


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)

I have map the Product and Warehouse 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


回答1:


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


来源:https://stackoverflow.com/questions/2455473/fluent-nhibernate-many-to-many-mapping-with-extra-column

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