Search code examples
c#nhibernatefluent-nhibernatenhibernate-mappingfluent-nhibernate-mapping

How to map an entity to entitycollection dictionary in fluent nhibernate


I have this classes (after removing redundant information from them):

public class Category : BaseEntity
{
    public virtual Category ContainingCategory { get; set; }
    public virtual IList<CategoryProperty> Properties { get; set; }
}

public class CategoryProperty : BaseEntity
{
    public virtual string Name { get; set; }
    public virtual IList<CategoryPropertyValue> Values { get; set; }
}

public class CategoryPropertyValue : BaseEntity
{
    public virtual string Name { get; set; }
}

public class CategoryPropertyValueCollection : BaseEntity
{
    public virtual IList<CategoryPropertyValue> Values { get; set; }
}

public class Product : BaseEntity
{
    public virtual string Name { get; set; }
    public virtual Category ContainingCategory { get; set; }
    public virtual IDictionary<CategoryProperty, CategoryPropertyValueCollection> Properties { get; set; }
}

And those are my mapping (after removing redundant information again):

public class CategoryMap : BaseMap<Category>
{
    public CategoryMap()
    {
        Map(x => x.Name);
        HasMany(x => x.Properties);
    }
}

public class CategoryPropertyMap : BaseMap<CategoryProperty>
{
    public CategoryPropertyMap()
    {
        Map(x => x.Name);
        HasMany(x => x.Values);
    }
}

public class CategoryPropertyValueMap : BaseMap<CategoryPropertyValue>
{
    public CategoryPropertyValueMap()
    {
        Map(x => x.Name);
    }
}

public class CategoryPropertyValueCollectionMap : BaseMap<CategoryPropertyValueCollection>
{
    public CategoryPropertyValueCollectionMap()
    {
        HasMany(x => x.Values).Cascade.None();
    }
}

public class ProductMap : BaseMap<Product>
{
    public ProductMap()
    {
        Map(x => x.Name);
        HasMany(x => x.Properties).AsEntityMap();
    }
}

Something is wrong with my mappings, it's easy to see what I'm trying to accomplish (ebay will be a good example of that logic - products from categories that has properties that varies and possible values to filter by). This is that tables I'm getting (which aren't good):

create table Categories (
    Id INT IDENTITY NOT NULL,
   Name NVARCHAR(255) null,
   ContainingCategoryId INT null,
   primary key (Id)
)

create table CategoryProperties (
    Id INT IDENTITY NOT NULL,
   Name NVARCHAR(255) null,
   CategoryId INT null,
   primary key (Id)
)

create table CategoryPropertyValueCollections (
    Id INT IDENTITY NOT NULL,
   ProductId INT null,
   CategoryProperty_id INT null,
   primary key (Id)
)

create table CategoryPropertyValues (
    Id INT IDENTITY NOT NULL,
   Name NVARCHAR(255) null,
   CategoryPropertyId INT null,
   CategoryPropertyValueCollectionId INT null,
   OrderItemId INT null,
   CategoryProperty_id INT null,
   primary key (Id)
)

Why does 'CategoryPropertyValues' is created with a 'CategoryPropertyValueCollectionId' column? What am I doing wrong in the mapping?


Solution

  • Found out my problem was the usage of HasMany instead of HasManyToMany.