How to use Attibute mapping with a ManyToMany Relationship with nHibernate
I have been using nHibernate and MVC to create application for my current client. For different reasons, the development team decided to go with attribute level mapping with entites vs fluent nhibernate or hbm mapping.
This document will show you how you can achieve that for a ManyToMany and ManyToOne and OneToMany Relationship. I have used sqlExpress. You can use any database you like. The downloadable project will have scripts to create the database for you.
Entity1 : First Entity
Entity2 : Second Entity.
XREF : Mapping Table with Entity1 And Entity2 with NO ADDITIONAL fields.
XREFPlus : Mapping table between Entity1 and Entity2 WITH ADDITIONAL fields.
Things to Know About Mapping.
XREF is a purely mapping table to facilitate a many to many relationship. If you find yourself in this situation, you do not need to create any entity class for that table. Your relational mapping with create the script on the fly to enter on that table.
But. XREFPlus is different. Since it has more fields than just the Foreign Keys. It is an One-to Many relation for both the entities. You will have to create a class for this and map this as One-To_Many and Many- to-One.
I have used Codesmith to create my basic project. Code smith doesn’t support attribute mapping yet. So This is how I created my own. Hopefully, If you are in the same show as I was, It will help you. I am trying to keep this short. Take a look at the included zip file and play around with it. Have fun.
UPDATE : Go to here for source Code.