Reputation: 4511
I have a set of tables that are mapped through reference tables (Products, Stores and StoreProducts)
Tables
Table: Product
---------------
Name
Id
Desc
Table: Stores
---------------
Name
Id
ZipCode
Table: StoreProduct
---------------
Id
StoreId
ProductId
isAvailable
ShelfID
Models
public class Store
{
public int Id {get;set;}
public string Name {get;set;}
public List<Product> Products {get;set;}
}
public class Product
{
public int Id {get;set;}
public string Name {get;set;}
public bool isAvailable {get;set;}
public int ShelfId {get;set}
public List<Store> Stores {get;set;}
}
Mappings
public class StoreMap: ClassMap<Store>
{
public StoreMap()
{
Id(x => x.Id);
Map(x => x.Name).Length(255).Nullable();
HasMany(x => x.Products)
.Table("StoreProduct")
.ParentKeyColumn("StoreId")
.ChildKeyColumn("ProductId");
}
}
public class ProductMap: ClassMap<Product>
{
public ProductMap()
{
Id(x => x.Id);
Map(x => x.Name).Length(255).Nullable();
HasMany(x => x.Stores)
.Table("StoreProduct")
.ParentKeyColumn("ProductId")
.ChildKeyColumn("StoreId");
}
}
I've looked at FluentNHibernate Lookup Table but cant see how to apply this to my structure. Am I going down the right lines? How do I map the StoreProduct table to each of the Domain models with the Fluent Mapping?
Secondly, how do I map a column from the reference lookup table to the child table (see the isAvailable column).
Upvotes: 0
Views: 984
Reputation: 2612
I think you have a many-to-many relationship here. A store contains many products, a product can be carried by more than one store.
something like this
public ProductMap()
{
Id(x => x.Id);
Map(x => x.Name).Length(255).Nullable();
HasManyToMany(x => x.Stores)
.AsBag()
.Table("StoreProduct")
}
Upvotes: 3