将 NHibernate 与 EAV 数据模型结合使用
我正在尝试利用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就这样
here goes