【问题标题】:Reference tables in FluentNHibernateFluentNHibernate 中的参考表
【发布时间】:2013-02-18 17:16:36
【问题描述】:

我有一组通过引用表(Products、Stores 和 StoreProducts)映射的表

表格

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 Lookup Table,但看不到如何将其应用于我的结构。我走的是正确的路线吗?如何使用 Fluent 映射将 StoreProduct 表映射到每个域模型?

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

【问题讨论】:

    标签: c# nhibernate fluent-nhibernate


    【解决方案1】:

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

    类似的东西

    public ProductMap()
    {
        Id(x => x.Id);
        Map(x => x.Name).Length(255).Nullable();
        HasManyToMany(x => x.Stores)
            .AsBag()
            .Table("StoreProduct")
    }
    

    【讨论】:

    • 谢谢,我会测试一下并根据需要更新问题/接受答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-23
    • 2019-07-29
    相关资源
    最近更新 更多