Configuring NHibernate with code

You can also configure NHibernate entirely in code. In this recipe, we'll show you how to do just that.

Getting ready

  1. Complete the steps in the Installing NHibernate recipe.
  2. Add a console application project to your solution called ConfigByCode.
  3. Set it as the Startup project for your solution.
  4. Install NHibernate to ConfigByCode project using NuGet Package Manager Console.
  5. In ConfigByCode, add a reference to the Eg.Core project.

How to do it…

  1. Add an App.config file with this configuration:
    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <connectionStrings>
        <add name="db" connectionString="Server=.\SQLEXPRESS; Database=NHCookbook; Trusted_Connection=SSPI" />
      </connectionStrings>
    </configuration>
  2. In Program.cs, add the ...

Get NHibernate 4.x Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.