Fluent NHibernate Composite Key Many-to-Many

Summary

I’m on a roll today, so I decided to create a new connection table between my Class table and Student table in my previous blog posts.  This table is identical to the StudentClass table, except I removed the primary key and used only two fields and made them into a composite key.  If you’re looking for an example of the composite key mapping, this is not the article to show it.  Even though I made the two connecting keys into one primary key, there is no composite mapping because the table itself is not mapped.


The ERD

The new table is called Student2Class and here is the final diagram:


Again, I’m using two databases because that was how my original tables were setup in the multiple database mapping article.  You can put all the tables in one database and perform the same CRUD operations that I have here.


Code Changes

I altered the name of the table in the mappings inside both the ClassMap and StudentMap objects.  That was my only changes from the previous project.  All the CRUD operations work as before.


You can download the sample code here:

FluentNHibernateManyToManyComposite.zip

 

Leave a Reply