Search code examples
nhibernatenhibernate-mappinghibernate-mappingnhibernate-mapping-by-code

Convert XML mappings to Code Mapping for ISET of ManyToMany


I am presently working on one of the project, where I need to convert the xml mappings to code mappings.

I have a ISET collection with Many to Many along with where clause. I had done the code mappings but where to put the where clause in code mappings ?

<set inverse="true" name="SystemRoles" table="UserPriv" mutable="true">
    <cache usage="read-write" />
    <key>
        <column name="UserID" />
    </key>
    <many-to-many
       class="SampleProject.Domain.SystemRole, SampleProject.Domain"
       where="PrivilegeType = 'SystemRole'">
        <column name="PrivilegeID" />
    </many-to-many>
</set>

And my Code mappings :

Set(x => x.SystemRoles, m =>
    {
        m.Schema("dbo");
        m.Table("UserPriv");
        m.Inverse(true);
        m.Key(k => k.Column("UserId"));
        m.Cascade(Cascade.None);
    }, col => col.ManyToMany(p =>
    {
        p.Column(x => x.Name("PrivilegeId"));
    })
    );

Where should I put : where="PrivilegeType = 'SystemRole'"


Solution

  • The where should belong to many-to-many as in the xml mapping

    Set(x => x.SystemRoles, m =>
        {
            // set mapping
        }, 
        col => col.ManyToMany(p =>
        {
            // mapping of the many-to-many
            p.Column(x => x.Name("PrivilegeId"));
            p.Where("PrivilegeType = 'SystemRole'");
        })
        );
    

    But not fully sure, if all features are already supported in mapping by code...

    Some more "documentation" http://notherdev.blogspot.com/2012/01/mapping-by-code-onetomany-and-other.html by Adam Bar

    EXTEND, check the https://github.com/nhibernate/nhibernate-core/blob/master/releasenotes.txt

    and for Build 4.0.0.Alpha1

    • [NH-2997] - Where() clause with many-to-many relation is missing (solution in description)