0

I started learning NHibernate since 2 weeks and i've read the book NHibernate 3 beginners guide. Unfortunately the book is written on the 3.1 version of it and since 3.2 some things has changed. No castle support anymore, ..

I had hoped to find some example-application or tutorial in 3.2, but can't find something like that anywhere. The most things are actually blog items about Nhibernates 3.2 replacement for FNH.

Since this afternoon the sessionfactory and stuff works without giving any errors (using session per request approach with a actionfilter) but i can't get my database generated. I'm mapping the classic way with .hbm.xml files for each entity. After reading a lot of websites and blogs i finally made this configuration:

    protected void Application_Start(object sender, EventArgs e)
    {
        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);

        var nhConfig = new Configuration().Configure();
        nhConfig.AddAssembly(typeof(Book).Assembly);
        BuildSchema(nhConfig);

        SessionFactory = nhConfig.BuildSessionFactory();
    }

    private static void BuildSchema(Configuration nhConfig)
    {
        new SchemaExport(nhConfig).Execute(false, true, false);
    }

My relevant web.config code looks like this:

   <connectionStrings>
    <add name="db" connectionString="Server=.\SQLExpress; Database=NHbTest; Trusted_Connection=SSPI"/>
   </connectionStrings>

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
        <session-factory>
            <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
            <property name="dialect">NHibernate.Dialect.MsSql2008Dialect,NHibernate</property>
            <property name="connection.connection_string_name">db</property>
            <property name="adonet.batch_size">100</property>
            <property name="current_session_context_class">web</property>
        </session-factory>
   </hibernate-configuration>

Someone sees what i'm doing wrong? Thx in advance.

Gigi2m02
  • 1,238
  • 3
  • 17
  • 33
  • Before you use NH 3.2 I'd strongly recommend looking into Fluent NHibernate. Currently it only is officially released with NH 3.1 support but it's such a better experience.. – Shane Courtrille Mar 09 '12 at 21:51
  • 3.2 doesn't support FNH because of it's internal code-mapping way. It would be kinda stupid to work with an old version of Nhibernate if there is a newer one, isn't it? And ofcourse: I'd rather want to do this with XML mapping in .hbm.xml files because of thesis reasons. – Gigi2m02 Mar 09 '12 at 22:00
  • FNH does support 3.2 unofficially. The official support is.. coming. And honestly I doubt I'll ever touch NH if I can't use FNH because it makes such a massive difference in usability. That said if you have a thesis reason to use .hbm.xml files.. I hope your thesis is about how crappy xml is as a configuration language? ;) – Shane Courtrille Mar 09 '12 at 22:03
  • Are you seeing any errors or it just doesn't work? Have you tried telling it to output the files so you can look at them? – Shane Courtrille Mar 09 '12 at 22:04
  • No errors. It just doesn't create my schema. – Gigi2m02 Mar 09 '12 at 22:08
  • I found the problem. I had forgotten to give the .hbm.xml files the property **'embedded resource'** instead of 'content' in build action. What a lame 'mistake'. Tried everything out, works well now. Withing 5 hours i can answer my own question. Will do it then. Thx for your replies. – Gigi2m02 Mar 09 '12 at 23:52
  • scrap the XML and use fluent mappings. Save yourself some headache. As far as using action filters for session management. Not exactly a good way to go. Several blog posts out there about better ways to do per request session management, – CrazyCoderz Mar 10 '12 at 14:48
  • You also don't have any caching setup in your xml config. – CrazyCoderz Mar 10 '12 at 14:50

1 Answers1

0

I found the problem. I had forgotten to give the .hbm.xml files the property 'embedded resource' instead of 'content' in build action. What a lame 'mistake'. Tried everything out, works well now.

Thx for your replies.

Gigi2m02
  • 1,238
  • 3
  • 17
  • 33