Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Alan Northam



Custom Profile Provider with NHibernate

    I've updated the NHibernate blog example code to include a custom profile provider.  This provider stores the profile information using the XML data type in SQL Server 2005.  New profile properties can be added to the web.config file in same way as the Microsoft SqlProfileProvider.  The included web.config contains examples showing how to add new properties and their types.  If adding a custom type be sure to include the full type name (i.e. NHBlog.BusinessEntities.Security.Application.).

    The example now includes custom NHibernate based providers for Membership, Roles, and Profiles.



Comments

jack said:

After setting up the database and building the solution, I launched the web site; and then I started to sign up a user, but soon after I clicked the button I got a very common .NET exception:

Server Error in '/NHBlog.WebUI' Application.

--------------------------------------------------------------------------------

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 92: public override string ApplicationName

Line 93: {

Line 94: get { return application.ApplicationName; }

Line 95: set { application = new DaoFactory().GetApplicationDao().GetByApplicationName(value); }

Line 96: }

Source File: G:\Resources\orm\NHibernate\NHBlog\NHBlog.DataAccess\Security\NHibernateMembershipProvider.cs    Line: 94

# August 14, 2007 4:46 AM

Alan said:

Jack:

I forgot to mention that you have to manually create an application in the applications table.  Add one named NHBlog and that should fix your error.

# August 14, 2007 9:41 AM

Tommy said:

I can't get this to work. It complaints about the membership providers type attribute in web.config. The error isn't in english but I guess an attempted translation to english would be something like "configuration error, failed under activation" (ish).

# August 16, 2007 6:46 AM

jack said:

Alan, you rock, now error fixed! Thanks for this great series, and I am looking forward the next post... :P

- Jack

# August 16, 2007 11:16 PM

Alan said:

Tommy:

I don't think you are getting the real cause of the error in the output you are seeing.  Run dbgview (www.microsoft.com/.../debugview.mspx) while testing so you can capture the log4net output from NHibernate.  Look for the first exception thrown and it should give you a better idea of what's causing the problem.

# August 17, 2007 9:16 AM

Gerard Alberts said:

Hi great reading I got this working after all, will analyze it further this weekend to really get NHibernate under my thumb. There's a small error in PostDetail.aspx.cs: here's the correct Page_Load, works if you replace it:

   protected void Page_Load(object sender, EventArgs e)

   {

       Post post = new DaoFactory().GetPostDao().GetById(postID);

       if (post != null && post.Comments == null)

       {

           foreach (Comment c in post.Comments)

           {

               log.Debug(c.Depth);

           }

       }

   }

# September 6, 2007 3:48 PM

Alan Northam said:

Gerard,

Thanks for pointing that out.  I made some major changes to the underlying code in this project and was in a bit of a hurry to refactor the presentation.  You will probably find some more odd looking code :)

# September 10, 2007 12:19 PM

Bryan Bass said:

Alan,  

Thanks for all this work. The ZIP attached to this article appears to be corrupt or invalid.

# April 25, 2008 3:11 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!

Our Sponsors

Proudly Partnered With