FluentNhibernate词典<;实体,ValueObject>;

FluentNhibernate词典<;实体,ValueObject>;,nhibernate,fluent-nhibernate,Nhibernate,Fluent Nhibernate,我有一个IDictionary属性的映射,这是映射: HasMany<StocksLocation>(mq => mq.StocksLocation) .KeyColumn("IDProduct") .AsEntityMap("IDLocation") .Element("Quantity", qt => qt.Type<decimal>()); HasMany(mq=>mq.StocksLocat

我有一个
IDictionary
属性的映射,这是映射:

    HasMany<StocksLocation>(mq => mq.StocksLocation)
        .KeyColumn("IDProduct")
        .AsEntityMap("IDLocation")
        .Element("Quantity",  qt => qt.Type<decimal>()); 
HasMany(mq=>mq.StocksLocation)
.KeyColumn(“IDProduct”)
.AsEntityMap(“空闲位置”)
.Element(“数量”,qt=>qt.Type());
现在我从
decimal
更改为Value对象:
Quantity

数量
有两个属性,十进制的
单位
单位(其中
单位
是一个枚举)

我现在必须映射
IDictionary
,如何实现这一点

提前感谢

选项1:将其映射为一个实体 我猜你的桌子看起来像这样:

CREATE TABLE Quantity (
    ID int NOT NULL,
    IDProduct int NOT NULL,
    IDLocation int NOT NULL,
    Value decimal(18,2) NOT NULL,
    Unit int NOT NULL,
    PRIMARY KEY (ID),
    FOREIGN KEY (IDProduct) REFERENCES Product (ID),
    FOREIGN KEY (IDLocation) REFERENCES StocksLocation (ID),
    UNIQUE KEY (IDProduct, IDLocation)
);
继续并将
Quantity
映射为实体类:

public class QuantityMap : ClassMap<Quantity>
{
    public QuantityMap()
    {
        Id(x => x.Id);
        References(x => x.Product, "IDProduct");
        References(x => x.Location, "IDLocation");
        Map(x => x.Value);
        Map(x => x.Unit);
    }
}
选项2:将其映射为组件

因为您认为您不希望将代码>代码>数量/代码>作为实体,让我们考虑如何将此映射为组件。

Product.StocksLocation
字典的*.hbm.xml映射如下所示:

<map name="StocksLocation" table="Quantity">
    <key column="IDProduct" />
    <index-many-to-many column="IDLocation" class="YourNamespace.StocksLocation, YourAssembly" />
    <composite-element class="YourNamespace.Quantity, YourAssembly">
        <property name="Unit" type="YourNamespace.Unit, YourAssembly" />
        <property name="Value" type="System.Decimal, mscorlib" />
    </composite-element>
</map>

我们如何使用FluentNHibernate?据我所知,在主干中没有这样做的方法,因此您有几个选项:

  • 实现了一个
    HasManyComponent
    方法。他有一个项目源代码的链接,但我不知道该源代码是否包含他对FluentNHibernate所做的更改
  • 如果他的更改源不可用,请随意实现您自己对FluentNHibernate的修改,并通过Github将其提交回社区
  • 如果这听起来太麻烦的话,FluentNHibernate在其他一切都失败时有一个最终的退路。它允许您混合和匹配各种映射方法。自动映射某些类,为其他类编写
    ClassMap
    类,并为无法使用FluentNHibernate映射的任何类编写*.hbm.xml文件

  • 我不知道创建一个实现IUserType是否是一个可行的解决方案,我必须更多地研究自定义类型的用法。有可能吗?然后o将有:HasMany(mq=>mq.StocksLocation).KeyColumn(“IDProduct”).AsEntityMap(“IDLocation”).Element(“Quantity”,qt=>qt.Type());它会要求数量映射吗?我会尝试一下…因为它是一本字典,你是说
    有很多(…)
    而不是
    有很多(…)
    ?谢谢,这是一个有效的选项,但我会尽量避免映射数量,因为它是一个值对象,如果我做不到,我会尝试那个选项。我编辑了我的答案,添加将
    数量
    映射为组件而不是实体的说明。
    <map name="StocksLocation" table="Quantity">
        <key column="IDProduct" />
        <index-many-to-many column="IDLocation" class="YourNamespace.StocksLocation, YourAssembly" />
        <composite-element class="YourNamespace.Quantity, YourAssembly">
            <property name="Unit" type="YourNamespace.Unit, YourAssembly" />
            <property name="Value" type="System.Decimal, mscorlib" />
        </composite-element>
    </map>