0

我有一组通过引用表(产品、商店和商店产品)映射的表

Table: Product
---------------
Name
Id
Desc


Table: Stores
---------------
Name
Id
ZipCode

Table: StoreProduct
---------------
Id
StoreId
ProductId
isAvailable
ShelfID

楷模

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

映射

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

    }
}

我查看了FluentNHibernate 查找表,但看不到如何将其应用于我的结构。我走的是正确的路线吗?如何使用 Fluent 映射将 StoreProduct 表映射到每个域模型?

其次,如何将参考查找表中的列映射到子表(参见 isAvailable 列)。

4

1 回答 1

3

我认为你在这里有一个多对多的关系。一个商店包含许多产品,一个产品可以被多个商店携带。

像这样的东西

public ProductMap()
{
    Id(x => x.Id);
    Map(x => x.Name).Length(255).Nullable();
    HasManyToMany(x => x.Stores)
        .AsBag()
        .Table("StoreProduct")
}
于 2013-02-15T16:43:56.027 回答