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.