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
MappingWithFluent
to theMappingRecipes
project.Add a reference to the
FluentNHibernate
package using Nuget package manager console.Create a new class named
ProductMap
with 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...