After my data access debacle at c4c I decided to sit down and give NHibernate a serious look again… so I can feel comfortable with it. It was still bothering me that I had to deal with xml configuration files (In addition to app.config/web.config). I really having to hunt down so many config files.
Alas, the answer for that problem is Fluent NHibernate. I had heard the name mentioned before but I didn’t know what it was. Basically, there is no need to create mapping xml files, you just create mapping classes using lambda expressions in code. This gives you intellisense (yay for us lazy people)!
So to start learning Fluent NHibernate I was looking through the ‘first project’ example and remembered something Lee Brandt told me at C4C, “you can generate the sql ddl from within NHibernate to deploy your sql schema”. Fantastic! I wondered if I could do this with Fluent NHibernate too.
Yes you can create your database from code using Fluent NHibernate… here is basically the snippet of code that had to change from the “First Project” example linked above:
private static ISessionFactory CreateSessionFactory()
.ConnectionString(c => c
.ExposeConfiguration(cfg => new SchemaExport(cfg)
Now, you would only run this program once in this case because during the creation of the session factory we are generating the database (which would probably fail if you ran the program again).
My thought is pulling it out into a method of it’s own to initiate only if the database doesn’t exist yet. Also, you could work in asking the user to enter in the connection information for the database server.