简体   繁体   English

流利的nHibernate映射HasMany具有固定值的复合键

[英]Fluent nHibernate Mapping HasMany Composite Key with Fixed Value

I'm trying to implement nHibernate mapping for a large exisiting system. 我正在尝试为大型现有系统实现nHibernate映射。 trying to map the relationship between two object. 试图映射两个对象之间的关系。 One object is "Attachments" which relate to a number of different objects across the system. 一个对象是“附件”,它与系统中的许多不同对象有关。 So in the database it has two columns which it uses to relate itself 因此,在数据库中,它有两列用于关联自身

ItemType 物品种类

ItemId ItemId

If I have a product with attachments on then ProductId = ItemId and ItemType will be a predefined value Eg '0001' Where as User object might be '0002', Order might be '0003' and so on for other ItemTypes. 如果我的产品带有附件,则ProductId = ItemId,并且ItemType将是预定义的值,例如'0001',其中User对象可能是'0002',Order可能是'0003',依此类推。

Now I need to map this in nhibernate. 现在我需要在nhibernate中映射它。 So I want to have a collections of attachments on the Product Object but this means mapping it across both ItemId and ItemType 所以我想在Product Object上收集附件,但这意味着将其跨ItemId和ItemType映射

If it was just the ItemId mapping that was required it could do 如果仅需要ItemId映射,则可以执行

HasMany(x => x.Attachments).KeyColumn("ProductId");

But Instead I need to map it where KeyColumn "ProductId" and ItemType in Attachments table is equal to '0001' 但是相反,我需要将其映射到附件表中的KeyColumn“ ProductId”和ItemType等于'0001'的位置

How could I go about this...? 我该怎么办...?

Structure Product table 结构产品表

[Product] [产品]

ProductId 产品编号

Name 名称

Description 描述

Attachment table 附件表

[Attachment] [附件]

AttachmentURL 附件网址

ItemId ItemId

ItemType 物品种类

What I would do is create a base, abstract Attachment type like so: 我要做的是创建一个基本的抽象附件类型,如下所示:

public abstract class Attachment
{
    public TYPE AttachmentURL { get; set; }
}

Then make a subclass for each attachment 'type': 然后为每个附件“类型”创建一个子类:

public class ProductAttachment : Attachment
{
    protected ProductAttachment() { }

    public ProductAttachment(Product parent)
    {
        Parent = parent;
    }

    public Product Parent { get; protected set; }
}

Then, in your Attachment mapping you will include this line: 然后,在附件映射中,您将包括以下行:

DiscriminateSubClassesOnColumn("ItemType");

And then add a 'DiscriminatorValue' call to each of the subclass mappings. 然后将“ DiscriminatorValue”调用添加到每个子类映射。 For example: 例如:

// Inside ProductAttachment mapping
...
DiscriminatorValue("0001");
...

Then, just map everything as you would normally. 然后,只需照常绘制所有内容即可。 This gives the added benefit that your Product will have a list of 'ProductAttachment' which would require the 'parent' to be a Product. 这带来了额外的好处,即您的产品将具有“ ProductAttachment”列表,这将要求“父级”成为产品。

There might be some weirdness when getting just the base type 'Attachment' (as opposed to 'ProductAttachment') from the database but you can cross that bridge when/if it comes. 从数据库中仅获取基本类型“附件”(而不是“ ProductAttachment”)时可能会有些怪异,但是当/如果有这种情况,您可以跨越该桥梁。

Found the answer here 在这里找到答案

Fluent Nibernate putting a where clause in the mapping 流利的Nibernate在映射中放置where子句

I can hard code the mapping like so 我可以像这样对映射进行硬编码

HasMany(x => x.Children).Where("ItemType='0001'");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM