我有 2 个具有多对多关系的表。
实体代码
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);
}
}
我在一致性映射中将这些类映射为多对多。
relationalMapper.ManyToMany<Product, Category>();
在 xml 中,此映射编译为:
<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>
问题是我可以从产品实体中获取类别,但是当我尝试从类别中获取产品时它不起作用并且列表为空。