流利Nhibernate多到多映射与额外的列 [英] Fluent Nhibernate Many-to-Many mapping with extra column
问题描述
我想用这个流利的Nhibernate映射这个,但是我不知道如何映射库存表
这是我拥有的表格:
产品(Id,Name,...)
仓库(Id,Name,...)
库存(Product_id,Warehouse_id,StockInHand)
$ b
公共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);
$ b我面对的问题是我该如何映射StockInHand(库存模型应该如何映射?)。
还是有其他方法来模拟这个场景吗?
我已经阅读了一些现有的问题,但还没有弄清楚该怎么做。
谢谢
你必须把它映射成两个一对多的关系,作为实体的库存。
类似于(我跳过了其他属性):
public class Product
{
public List< Inventory>库存{get;组; }
}
public class Warehouse
{
public List< Inventory>库存{get;组; }
}
public class Inventory
{
public Product Product {get;组; }
public仓库仓库{get;组; }
public bool StockInHand {get;组; }
}
Product ProductMap(){
Id(x => x.Id);
Map(x => x.Name);
HasMany(x => x.Inventory)
.Cascade.All()
.Inverse()
.Table(Inventory);
$ b $ public WarehouseMap()
{
Id(x => x.Id);
Map(x => x.Name);
HasMany(x => x.Inventory)
.Cascade.All()
.Inverse()
.Table(Inventory);
$ b public InventoryMap()
{
CompositeId()
.KeyReference(x => x.Product,Product_id)
.KeyReference(x => x.Warehouse,Warehouse_id)
Map(x => x.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)
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
解决方案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); }
这篇关于流利Nhibernate多到多映射与额外的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!