Click here to Skip to main content
11,634,827 members (69,435 online)
Click here to Skip to main content

Nhibernate .NET Providers

, 2 Apr 2009 Ms-PL 19.2K 600 18
Rate this:
Please Sign up or sign in to vote.
NHibernate based Profile, Membership, Role Providers written in C#


Hibernate is a large scale project which covers 95 percent of common data persistence related programming tasks about SQL and database. We can call hibernate as one upper level of database(OULOD). I am kidding, we don't need any more terminologic and cryptic acronyms for making our documents hard to read as hell.

NHibernate is the port of Hibernate to .NET world. I don't know what developers think about ORMs after LINQ which makes these kind of things very very easy and neat. Well this article will introduce you to nice and dandy nhibernate and implements .NET membership, role, and profile providers.

About a week ago, I saw this project. It did not have ProfileProvider and I started digging and writing my implementation of ProfileProvider. It did not tested under heavy load. Just a starting point.


You don't have to read it (because I don't believe reading documents takes you anywhere). But if you really wonder what this hibernate is, you can read it here.

Class Diagram

As you can see, it's a pretty simple and basic nhibernate project. The main provider inherited classes are:

  1. NHibernateMembershipProvider: handles the membership provider related tasks from abstracted level.
  2. NHibernateProfileProvider: I implemented this from real sqlprovider decompiled classes. I have not tested every aspect of this class. Please do that and send me bugs.
  3. NHibernateRoleProvider: Done the role provider implementation for .NET 2.0 class.

class diagram of the nhibernate providers

Using the Provider

Step 1: You need to make your database ready. I use mysql for this article and the code tested under mysql. Create a database named nhibernateprovidertest.

Step 2: Run the script which comes with the zip file named NHibernateProviderDataModel - mysql.ddl under NHibernateProviderDatabase\Create Scripts. I know this can be tricky. Even I get errors and swear the GUI tools. But it's fast and open source database and I like it.

Step 3: Open NHibernateProvider.sln with VS.NET 2008. and find web.config file and change the connection string at line:140 to point to your mysql server.

That is all. Now you have nhibernate which works on most famous databases (in theory). Click on the "Play" button. Smile | :)

Database Design

I must say that it was already designed for me. I just added one more table to store profile data. We have 7 table names of those:

  1. ApplicationUserRole: Connection table for user and roles. Holds just application id, user id and role id
  2. ApplicationUser: Contains which user belongs to which application. Holds application id and user id
  3. ApplicationRole: Contains which role belongs to which application. Holds application id and role id
  4. Role
  5. MembershipUser: Holds initial user data
  6. Application
  7. Profiles: columns:
    1. PropertyNames
    2. PropertyValuesString
    3. LastActivityDate
    4. PropertyValuesBinary

nhibernate providers

First, I was afraid for there was no connection between profiles and any other table. But then I thought that there are a lot of connections between other tables for more work. If I put any more connections between profile and any table, this means database has to check foreign keys when any delete or insert appears.

Points of Interest

If you develop any nhibernate project, don't forget to embed your mapping files into build. Otherwise you will read a lot of forum entry and solving this took longer then you would expect. Just select your mapping file "any.hbm.xml" and press F4 to open properties dialog and select "Embedded Resource" in "Build Action" line.

I have not used nhibernate in any of my projects. With this tool, I plan to support any database as much as I can. This article and code was a start. My next target is SQLite. Don't hesitate to join me. I am open to any suggestions/testing/debugging.


  • 2009-04-01 First release


This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


About the Author

Software Developer (Senior)
Turkey Turkey

You may also be interested in...

Comments and Discussions

GeneralMy vote of 4 Pin
okdone22-Sep-11 22:05
memberokdone22-Sep-11 22:05 
GeneralMy vote of 1 Pin
trevorde stickman1-Apr-09 10:24
membertrevorde stickman1-Apr-09 10:24 
GeneralMy vote of 1 Pin
pita20091-Apr-09 8:42
memberpita20091-Apr-09 8:42 
GeneralMy vote of 1 Pin
Puchko Vasili1-Apr-09 6:55
memberPuchko Vasili1-Apr-09 6:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150728.1 | Last Updated 2 Apr 2009
Article Copyright 2009 by ozkan.pakdil
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid