首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >FluentNhibernate IDictionary<Entity,ValueObject>

FluentNhibernate IDictionary<Entity,ValueObject>
EN

Stack Overflow用户
提问于 2010-03-11 18:32:31
回答 1查看 1.4K关注 0票数 4

我有一个IDictionary<StocksLocation,decimal>属性的映射,这是一个映射:

代码语言:javascript
运行
复制
    HasMany<StocksLocation>(mq => mq.StocksLocation)
        .KeyColumn("IDProduct")
        .AsEntityMap("IDLocation")
        .Element("Quantity",  qt => qt.Type<decimal>()); 

现在,我从decimal更改为值对象:Quantity

Quantity有两个属性,十进制ValueUnit单位(其中Unit是枚举)。

我现在必须映射IDictionary<StocksLocation,Quantity>,我如何实现这一点?

提前感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-03-18 11:38:00

选项1:将其映射为实体

我猜你的表看起来像这样:

代码语言:javascript
运行
复制
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映射为实体类:

代码语言:javascript
运行
复制
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);
    }
}

..。然后将Product.StocksLocation映射更改为:

代码语言:javascript
运行
复制
HasMany<StocksLocation, Quantity>(mq => mq.StocksLocation)
    .KeyColumn("IDProduct")
    .AsMap(x => x.Location); 

选项2:将其映射为组件

因为您说过不希望将Quantity映射为实体,所以让我们考虑如何将其映射为组件。Product.StocksLocation字典的*.hbm.xml映射如下所示:

代码语言:javascript
运行
复制
<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做到这一点呢?据我所知,在主干中没有这样做的方法,所以你有几个选择:

  1. Gabriel Schenker实现了一个HasManyComponent方法。他有一个到他的项目源代码的链接,但我不知道那个源代码是否包括他对FluentNHibernate.
  2. If所做的更改。他的更改的源代码是不可用的,请随意实现您自己对FluentNHibernate的修改,并通过Github将它们提交回社区。
  3. 如果这听起来太麻烦,当所有其他方法都失败时,FluentNHibernate有一个最终的后备。它允许您混合和匹配各种映射方法。自动映射某些类,为其他类编写ClassMap类,并为无法使用FluentNHibernate.

映射的任何类编写*.hbm.xml文件

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2424180

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档