문제

I'm struggling to wrap my head around the best way to approach mapping nHibernate to an existing database. Below are the basic tables:

User Table: UserID field1 field2 ...

Permission Table: PermissionID field1 field2 ...

UserPermissions: UserID PermissionID

I have the classes mapped (using XML) and functioning for the User and Permission tables but now want to create sets in both those classes using the two column mapping table.

that is, the Permission class would have a ICollection<User> and the User Class an ICollection<Permission> member.

I don't think I should map the UserPermission table to an Entity since it's not a true Entity. It's simply a mapping table.

Unfortunately looking though all of the nHibernate docs and doing searches hasn't lead me to a clear way of doing this. Any help would be appreciated.

도움이 되었습니까?

해결책

NHibernate has support for this kind of the DB design. The mapping in this case will use many-to-many element - without any explicit mapping of the pairing entity/table.

Here is an adjusted snippet coming from the documentation: 6.8. Bidirectional Associations

<class name="User">
    <id name="Id" column="UserID"/>
    ...
    <bag name="Permissions" table="UserPermissions" lazy="true">
        <key column="UserID"/>
        <many-to-many class="Permission" column="PermissionID"/>
    </bag>
</class>

<class name="Permission">
    <id name="Id" column="PermissionID"/>
    ...

    <!-- inverse end -->
    <bag name="Users" table="UserPermissions" inverse="true" lazy="true">
        <key column="PermissionID"/>
        <many-to-many class="User" column="UserID"/>
    </bag>
</class>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top