Search code examples
c#nhibernatemany-to-manyhbm

many to many relation with nhibernate on the entity


I have a problem with nhibernate,

i have a hbm file with a many to many relations. This works so far but i want to generate the hbm from a entity

and trying to set the properties on the entity but that will not compile

this is the hbm file

<list name="Category" table="Category" lazy="false" fetch="select" cascade="none">
  <key column="categoryId" />
  <index column="ordinal" />
  <many-to-many class="Product, Assembly" column="productId" />
</list>

and have did now on the entity

    [List(2, Name = "Product", Table = "Product", Cascade = CascadeStyle.None, Lazy = false, Fetch = CollectionFetchMode.Select)]
    [ManyToMany(5, ClassType = typeof(Product), Column = "productId")]
    public virtual IList<Category> Categorys

Solution

  • You forgot the ordinal and the key on the entity try this

        [List(1, Name = "Product", Table = "Product", Cascade = CascadeStyle.None, Lazy = false, Fetch = CollectionFetchMode.Select)]
        [NHibernate.Mapping.Attributes.Key(2, Column = "categoryId")]
        [Index(3, Column = "ordinal")]
        [ManyToMany(4, ClassType = typeof(Product), Column = "productId")]
        public virtual IList<Category> Categorys