c# - NHibernate relationship has an issue with two-directional getting data -
i have 2 tables 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 classes many-to-many in conform mapping.
relationalmapper.manytomany<product, category>();
in xml mapping compiles 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 can categories product entity, when try products category it's doesn't work , list empty.
i don't think can have list
on both sides of many-to-many
. 1 side can list
- other side should bag
or set
. consider following data in productcategory table:
category_id product_id index =========== ========== ===== 1 3 0 1 4 1 2 3 0 2 4 1
if access category.categoryproducts
, well. category #1 has 2 products: first product #3 , second #4.
however, if try access product.productcategory
, same index
column cannot used list. our data says product #3 has 2 categories: #1 , #2 - both of them want first category in list, index = 0
. product #4 has 2 categories, neither of them want first category in list because both have index = 1
.
the index values in list should sequential starting zero. don't think it's possible 2 lists driven same table.
Comments
Post a Comment