In this chapter, we will cover the following recipes:
Since all the recipes in this chapter deal with querying, we have provided the necessary setup and data using the NH4CookbookHelpers
library. You can easily adapt the recipes to your own liking, but if you want to only test the queries, the supplied setup will work just fine.
Windows Forms
project named QueryRecipes
in Visual Studio.Install-Package NHibernate
Form1.cs
from the project.using NH4CookbookHelpers;
to the top of Program.cs
.Program.cs
so that the last line in Main reads:Application.Run(new WindowsFormsRunner());
If you use NH4CookbookHelpers
, you don't even have to have a database server available, since it uses an in-memory SQLite database by default. If you want to use something else, like a local SQL Server, you can specify a Configuration
instance to be used. Perhaps the most convenient way is to use a coded configuration, as described in Chapter 1, The Configuration and Schema.
Somewhere prior to the last line in Main
, specify a default configuration, as shown:
RecipeLoader.DefaultConfiguration = () => new Configuration() .DataBaseIntegration(db => { db.Dialect<MsSql2012Dialect>(); db.Driver<Sql2008ClientDriver>(); db.ConnectionString = "Server=.SQLEXPRESS;Database=NHCookbook;Trusted_Connection=True;"; });
Please be advised that the database used in the recipes will be tinkered with, so don't use a database with data that you need to stay intact.
3.149.249.174