Search code examples
c#nhibernatedictionarynhibernate-mappingnhibernate-mapping-by-code

NHibernate MappingByCode Map Cascade options


I have the following class with a Dictionary :

public class Entity1 
{
    public Dictionary<Entity2,Entity3> Targets { get; set; }
}

Mapped currently by this code :

    Map(x => x.Targets,
        m => m.Key(k => k.Column("Entity1ID")),
        km => km.ManyToMany(mtm => mtm.Column("Entity2ID")),
        vm => vm.ManyToMany(mtm => mtm.Column("Entity3ID")));

I am looking for a way to set different cascade option to the KeyMapping, and a different one to the ValueMapping. But i can find cascade only in the main "Collection Mapping"

I want the key to have Cascade.None - because a different part of the application manages that entity, and the Value to have Cascade.AllDeleteOrphan.

How can i do that with MbC ?

plus - if i set the "CollectionMapping"'s Cascade - to what Entity does it reflect?

I am using the latest Nhibernate on nuget.


Solution

  • AFAIK Cascading options on Collections only effect the values of the collection. Having Cascade.All on the collection:

    var e2 = new Entity2();
    using (var tx = session.BeginTransaction())
    {
        session.Save(e2);
    
        session.Save(new Entity1 { Dictionary = { { e2, new Entity3() } } }); // should work
        session.Save(new Entity1 { Dictionary = { { new Entity2(), new Entity3() } } }); // does not work
    
        tx.Commit();
    }