将 NHibernate 与 EAV 数据模型结合使用

发布于 2024-08-31 18:48:12 字数 1470 浏览 10 评论 0原文

我正在尝试利用 NH 映射到一个数据模型,该模型是对 EAV/CR 数据模型的松散解释。

我已经完成了大部分工作,但正在努力映射 Entity.Attributes 集合。

以下是有问题的表:

--------------------
| Entities         |
--------------------
| EntityId  PK     |-|
| EntityType       | |
-------------------- |
         -------------
         |
         V
--------------------
| EntityAttributes |    ------------------    ---------------------------
--------------------    | Attributes     |    | StringAttributes        |
| EntityId  PK,FK  |    ------------------    ---------------------------
| AttributeId  FK  | -> | AttributeId PK | -> | StringAttributeId PK,FK |
| AttributeValue   |    | AttributeType  |    | AttributeName           |
--------------------    ------------------    ---------------------------

AttributeValue 列被实现为 sql_variant 列,我已经为其实现了 NHibernate.UserTypes.IUserType。

我可以创建一个 EntityAttribute 实体并直接保留它,以便层次结构的该部分正常工作。

我只是不确定如何将 EntityAttributes 集合映射到 Entity 实体。

请注意,EntityAttributes 表可以(并且确实)包含给定 EntityId/AttributeId 组合的多行:

EntityId AttributeId AttributeValue
-------- ----------- --------------
1        1           Blue
1        1           Green

对于此示例,StringAttributes 行如下所示:

StringAttributeId AttributeName
----------------- --------------
1                 FavoriteColor

如何有效地将这个数据模型映射到我的实体域,以便 Entity.Attributes("FavoriteColors" ) 返回最喜欢的颜色的集合?输入为 System.String?

I'm trying to leverage NH to map to a data model that is a loose interpretation of the EAV/CR data model.

I have most of it working but am struggling with mapping the Entity.Attributes collection.

Here are the tables in question:

--------------------
| Entities         |
--------------------
| EntityId  PK     |-|
| EntityType       | |
-------------------- |
         -------------
         |
         V
--------------------
| EntityAttributes |    ------------------    ---------------------------
--------------------    | Attributes     |    | StringAttributes        |
| EntityId  PK,FK  |    ------------------    ---------------------------
| AttributeId  FK  | -> | AttributeId PK | -> | StringAttributeId PK,FK |
| AttributeValue   |    | AttributeType  |    | AttributeName           |
--------------------    ------------------    ---------------------------

The AttributeValue column is implemented as an sql_variant column and I've implemented an NHibernate.UserTypes.IUserType for it.

I can create an EntityAttribute entity and persist it directly so that part of the hierarchy is working.

I'm just not sure how to map the EntityAttributes collection to the Entity entity.

Note the EntityAttributes table could (and does) contain multiple rows for a given EntityId/AttributeId combination:

EntityId AttributeId AttributeValue
-------- ----------- --------------
1        1           Blue
1        1           Green

StringAttributes row looks like this for this example:

StringAttributeId AttributeName
----------------- --------------
1                 FavoriteColor

How can I effectively map this data model to my Entity domain such that Entity.Attributes("FavoriteColors") returns a collection of favorite colors? Typed as System.String?

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

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

发布评论

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

评论(1

花期渐远 2024-09-07 18:48:12

就这样

class Entity
{
    public virtual int Id { get; set; }

    internal protected virtual ICollection<EntityAttribute> AttributesInternal { get; set; }

    public IEnumerable<T> Attributes<T>(string attributeName)
    {
        return AttributesInternal
            .Where(x => x.Attribute.Name == attributeName)
            .Select(x => x.Value)
            .Cast<T>();
    }
}

class EntityAttribute
{
    public virtual Attribute Attribute { get; set; }

    public virtual object Value { get; set; }
}

class EntityMap : ClassMap<Entity>
{
    public EntityMap()
    {
        HasMany(e => e.AttributesInternal)
            .Table("EntityAttributes")
            .KeyColumn("EntityId")
            // EntityAttribute cant be an Entity because there is no real Primary Key
            // (EntityId, [AttributeId] is not unique)
            .Component(c =>
            {
                c.References(ea => ea.Attribute, "AttributeId").Not.LazyLoad();
                c.Map(ea => ea.Value, "AttributeValue").CustomType<VariantUserType>();
            });
    }
}

here goes

class Entity
{
    public virtual int Id { get; set; }

    internal protected virtual ICollection<EntityAttribute> AttributesInternal { get; set; }

    public IEnumerable<T> Attributes<T>(string attributeName)
    {
        return AttributesInternal
            .Where(x => x.Attribute.Name == attributeName)
            .Select(x => x.Value)
            .Cast<T>();
    }
}

class EntityAttribute
{
    public virtual Attribute Attribute { get; set; }

    public virtual object Value { get; set; }
}

class EntityMap : ClassMap<Entity>
{
    public EntityMap()
    {
        HasMany(e => e.AttributesInternal)
            .Table("EntityAttributes")
            .KeyColumn("EntityId")
            // EntityAttribute cant be an Entity because there is no real Primary Key
            // (EntityId, [AttributeId] is not unique)
            .Component(c =>
            {
                c.References(ea => ea.Attribute, "AttributeId").Not.LazyLoad();
                c.Map(ea => ea.Value, "AttributeValue").CustomType<VariantUserType>();
            });
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文