2010-05-06 11 views
15

Ich versuche NH zu nutzen, um ein Datenmodell zu erstellen, das eine lose Interpretation des EAV/CR-Datenmodells ist.Verwenden von NHibernate mit einem EAV-Datenmodell

Ich habe das meiste davon zu arbeiten, aber ich kämpfe mit der Zuordnung der Entity.Attributes Sammlung.

Hier sind die Tabellen in Frage:

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

Die Attribute Spalte als sql_variant Spalte implementiert ist, und ich habe eine NHibernate.UserTypes.IUserType für sie umgesetzt.

Ich kann eine EntityAttribute-Entität erstellen und diese direkt beibehalten, sodass ein Teil der Hierarchie funktioniert.

Ich bin mir nicht sicher, wie die EntityAttributes-Auflistung der Entity-Entität zugeordnet wird.

Notiere die EntityAttributes Tabelle konnte (und tut) mehrere Zeilen für eine bestimmte EntityID/AttributeId Kombination enthalten:

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

StringAttributes Zeile für dieses Beispiel wie folgt aussieht:

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

Wie kann ich dieses Datenmodell effektiv meiner Entitätsdomäne zuordnen, sodass Entity.Attributes ("FavoriteColors") eine Sammlung von Lieblingsfarben zurückgibt? Als System.String eingegeben?

+0

Sind Sie fließend mit? –

+0

Wenn Sie Entitäten nach Attributwerten suchen, bin ich mir nicht sicher, ob sql_variant ordnungsgemäß funktioniert. Du solltest das versuchen. –

Antwort

1

hier geht

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>(); 
      }); 
    } 
} 
Verwandte Themen