Geeks With Blogs

@leebrandt

News INETA Community Speakers Program

Yes We Kanban

View Lee Brandt's profile on LinkedIn

Addicted toThe Most Intelligent Add-In To Visual Studio


Lee Brandt's Blog You're only as smart as your last line of code

Today I needed to override the default convention settings in Fluent NHibernate. By default, fNH sets up the default key columns that it looks for as “[TableName]_id”. The convention that we decided on at my work is ID for the surrogate key column of the table and “[TableName]ID” for every place it is used as a foreign key.

 

In our RepositoryBase class is where we setup the session factory for NHibernate. This is also the place where you set up the PersistenceModel for fNH. Normally, what you might have is:

   1: var cfg = new Configuration().Configure();
   2: var persistenceModel = new PersistenceModel();
   3: persistenceModel.addMappingsFromAssembly(Assembly.Load("Examinetics.NHibernate.Data"));
   4: persistenceModel.Configure(cfg);
   5: factory = cfg.BuildSessionFactory();

But if you need to change some of the default conventions, try:

   1: var cfg = new Configuration().Configure();
   2: var persistenceModel = new PersistenceModel();
   3: persistenceModel.addMappingsFromAssembly(Assembly.Load("Examinetics.NHibernate.Data"));
   4: persistenceModel.Conventions.GetForeignKeyNameOfParent = type => type.Name + "ID";
   5: persistenceModel.Configure(cfg);
   6: factory = cfg.BuildSessionFactory();

The extra line sets the key column name when it is used as a foreign key.

There may be better ways to do this. This is pretty simple, but It was a tough find.

 

Hope this helps someone.

~Lee

Posted on Tuesday, September 2, 2008 6:46 PM Learning | Back to top


Comments on this post: Overriding the Default Conventions in Fluent NHibernate

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Lee Brandt | Powered by: GeeksWithBlogs.net | Join free