FluentNhibernate IDictionary

发布于 2024-08-25 03:05:52 字数 614 浏览 3 评论 0原文

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

    HasMany<StocksLocation>(mq => mq.StocksLocation)
        .KeyColumn("IDProduct")
        .AsEntityMap("IDLocation")
        .Element("Quantity",  qt => qt.Type<decimal>()); 

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

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

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

提前致谢

I had a mapping for a IDictionary<StocksLocation,decimal> property, this was the mapping:

    HasMany<StocksLocation>(mq => mq.StocksLocation)
        .KeyColumn("IDProduct")
        .AsEntityMap("IDLocation")
        .Element("Quantity",  qt => qt.Type<decimal>()); 

Now i changed from decimal to a Value Object: Quantity.

Quantity has two properties, decimal Value and Unit Unit (where Unit is an enum).

I now have to map IDictionary<StocksLocation,Quantity>, how can i achieve this?

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

手长情犹 2024-09-01 03:05:52

选项 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);
    }
}

...然后更改 Product.StocksLocation 映射到:

HasMany<StocksLocation, Quantity>(mq => mq.StocksLocation)
    .KeyColumn("IDProduct")
    .AsMap(x => x.Location); 

选项 2:将其映射为组件

因为您评论说您不想将 Quantity 映射为实体,所以让我们考虑如何将其映射为组件。 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>

How do we do this with FluentNHibernate?据我所知,没有一种方法可以在主干中执行此操作,因此您有几个选择:

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

Option 1: Map it as an Entity

I'm guessing that your table looks similar to this:

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

Go ahead and map Quantity as an entity class:

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

... and then change the Product.StocksLocation mapping to:

HasMany<StocksLocation, Quantity>(mq => mq.StocksLocation)
    .KeyColumn("IDProduct")
    .AsMap(x => x.Location); 

Option 2: Map it as a Component

Because you commented that you'd rather not map Quantity as an entity, let's consider how we would map this as a component instead. The *.hbm.xml mapping for the Product.StocksLocation dictionary would look like this:

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

How do we do this with FluentNHibernate? As far as I know, there isn't a method for doing this in the trunk, so you have a few options:

  1. Gabriel Schenker implemented a HasManyComponent method. He has a link to the source code for his project, but I don't know whether that source includes the changes he made to FluentNHibernate.
  2. If the source for his changes are not available, feel free to implement your own modifications to FluentNHibernate and submit them back to the community via Github.
  3. If that sounds like too much trouble, FluentNHibernate has an ultimate fallback when all else fails. It allows you to mix and match various mapping methods. Auto-map some of your classes, write ClassMap classes for others, and write a *.hbm.xml file for any classes that can't be mapped with FluentNHibernate.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文