I know NHibernate is an ORM and it isn’t normally used to create tables but I also know that NHibernate is able to create an entire database given some mappings.
I would like to know if there is an obscured API that I could use to dynamically create/alter/delete tables. I could do it with ADO.Net but I would like to abstract the code for creating tables for different databases (MS SQL, MySQL, etc.)
Precision 1: The problem with CreateSQLQuery is that I would have to rewrite the method for creating a table for different SQL servers (MS SQL, MySQl, etc.) It has no advantages over ADO.Net. When NHibernate generates the database from mappings it generates for any SQL servers… that is what I’m looking for. What is the code that is executed when NHibernate generates a database from mappings… is this code available/public?
Advertisement
Answer
Yes you can 🙂
You create dynamically a Type corresponding to the class to be mapped. And then generate a mapping.
The important classes to look in NH source code are in the Nhibernate.Mapping namespace : PersistentClass, RootClass.
Here is a sample :
https://nhibernate.info/blog/2008/11/16/mapping-source-how-map-a-class-without-use-nothing.html
I have used this “API” to generate dynamically Tables.