Creating mappings fluently
Even before NHibernate added the possibility to provide mappings in code, the Fluent NHibernate project (FNH) delivered a strongly-typed, fluent syntax, as an alternative to XML mappings. It remains very popular and many NHibernate articles online show examples using FNH mappings. In this recipe, we will show you how to map our product model using Fluent NHibernate.
Getting ready
Complete the Getting ready instructions at the beginning of this chapter.
How to do it…
Add a new folder named
MappingWithFluentto theMappingRecipesproject.Add a reference to the
FluentNHibernatepackage using Nuget package manager console.Create a new class named
ProductMapwith the following code:using NH4CookbookHelpers.Mapping.Model; using FluentNHibernate.Mapping; namespace MappingRecipes.MappingWithFluent { public class ProductMap : ClassMap<Product> { public ProductMap() { Id(p => p.Id).GeneratedBy.GuidComb(); Version(x => x.Version); NaturalId().Property...