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
- Complete the steps in the Installing NHibernate recipe.
- Add a console application project to your solution called
ConfigByCode
. - Set it as the Startup project for your solution.
- Install NHibernate to
ConfigByCode
project using NuGet Package Manager Console. - In
ConfigByCode
, add a reference to theEg.Core
project.
How to do it…
- 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>
- 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.