Search code examples
model-view-controllernhibernatemappingmany-to-manyconform

Mapping NHibernate with confORM


I have 2 classes (summarized for brevity) :

public class Product : Entity<Guid>
{      
    ...    
    public virtual IList<Ingredient> Ingredients { get; set; }          
    public Product(){Ingredients = new List<Ingredient>();}    
}

and

public partial class Ingredient : Entity<int>
{
    ...
    public virtual IList<Product> Products { get; set; }
    public Ingredient(){Products = new List<Product>();}
}

They have a ManyToMany relationship, and I want to do:

  • if I delete one ingredient, the product is not removed but only the ingredient for his list.
  • if I delete one product, the ingredients are all without being deleted.

I did this map, but I can't get this to work.

orm.ManyToMany<Product, Ingredient>();
orm.Cascade<Product, Ingredient>(CascadeOn.DeleteOrphans);

Solution

  • Finally, I got it. This is the way I could resolve this, in case to help someone more:

            orm = new ObjectRelationalMapper();
            mapper = new Mapper(orm);
            //...
    
            mapper.Class<Ingredient>(c =>
            {
               /* ...[MAP OTHERS PROPERTY]...*/
               // Many to many relationship in One side
                c.Bag(p => p.Products, pm => pm.Inverse(false), rel => rel.ManyToMany());
            });
    
           // Many to many relationship in other side
           orm.ManyToMany<Product, Ingredient>();