I have 2 tables which have many-to-many relation.
Code of entities
public class Product : BaseEntity
{
public virtual string Name { get; set; }
public virtual IList<Category> ProductCategory { get; set; }
public virtual float Price { get; set; }
public virtual string Description { get; set; }
public virtual DateTime DateOfAdd { get; set; }
public virtual float Discount { get; set; }
public virtual int SaleCount { get; set; }
public virtual byte[] Image { get; set; }
}
public class Category : BaseEntity
{
public virtual string Name { get; set; }
public virtual string Description { get; set; }
public virtual IList<Product> CategoryProducts { get; set; }
public virtual void AddProduct(Product product)
{
this.CategoryProducts.Add(product);
}
public virtual void DeleteProduct(Product product)
{
this.CategoryProducts.Remove(product);
}
}
I map this classes as many-to-many in the conform mapping.
relationalMapper.ManyToMany<Product, Category>();
In xml this mapping compiles into this:
<class name="Product">
<id name="Id" type="Int32">
<generator class="identity" />
</id>
<property name="Name" />
<list name="ProductCategory" table="ProductCategory">
<key column="product_key" />
<list-index />
<many-to-many class="Category" column="category_key" />
</list>
<property name="Price" />
<property name="Description" />
<property name="DateOfAdd" />
<property name="Discount" />
<property name="SaleCount" />
<property name="Image" lazy="true" />
</class>
<class name="Category">
<id name="Id" type="Int32">
<generator class="identity" />
</id>
<property name="Name" />
<property name="Description" />
<list name="CategoryProducts" table="ProductCategory" inverse="true">
<key column="category_key" />
<list-index />
<many-to-many class="Product" column="product_key" />
</list>
</class>
The issue is that I can get categories from product entity, but when I try get products from category it's doesn't work and the list is empty.
I don't think you can have a
list
on both sides of themany-to-many
. Only one side can be alist
- the other side should just be anbag
orset
. Consider the following data in the ProductCategory table:If you access
Category.CategoryProducts
, all is well. Category #1 has two products: the first product is #3 and the second is #4.However, if you try to access
Product.ProductCategory
, the sameIndex
column cannot also be used for this list. Our data says that Product #3 has two categories: #1 and #2 - but both of them want to be the first category in the list, withIndex = 0
. Product #4 also has two categories, but neither of them want to be the first category in the list because they both haveIndex = 1
.The Index values in a list should be sequential starting from zero. I don't think it's possible to do this for two lists driven by the same table.