Fluent NHibernate provides a Fluent API also enables you to use LINQ to query data on top of the NHibernate engine ORMs (object relational mappers) simplify data access in your application by allowing you to write code to perform CRUD (Create, Read, Update, and Delete) operations. ORM frameworks have been in use for a long time to eliminate the impedance mismatch that exists between the object and data models in an application. In essence, ORMs enable you to write code to perform CRUD operations sans the need of interacting with the underlying database provider directly. Thus, usage of ORMs help you to isolate the object model of your application from the data model. Why Fluent NHibernate? NHibernate stores the mapping information in XML format in .hbm files — you should have one .hbm file for each entity class. This .hbm file is used to map the entities to the corresponding database tables. In using Fluent NHibernate, you no longer need to use the cumbersome .hbm.xml files that you have had to use when working with NHibernate. Fluent NHibernate is the statically compiled, compile safe counterpart of the popular ORM tool NHibernate that can be used to create mapping between the POCO classes and NHibernate engine sans the need of cumbersome XML files. It provides a Fluent API also enables you to use LINQ to query data on top of the NHibernate engine. In the sections that follow, we will discuss how we can install Fluent NHibernate, create models, map these models or entity classes and use Fluent NHibernate to perform CRUD operations. Getting started To get started using Fluent NHibernate, follow these steps: Open Visual Studio 2015 IDE Click on File -> New -> Project Create a new project – for the sake of simplicity, create a Windows Application Specify a name for the project Click OK to save the project Now that a project has been created in Visual Studio, you may want to install Fluent NHibernate to use it in your application. If you have NuGet installed, the easiest option is to install Fluent NHibernate via the NuGet Package Manager. To do this select the project in the Solution Explorer Window, right click and select “Manage NuGet Packages…” option to install Fluent NHibernate framework from NuGet. Working with Fluent NHibernate To work with Fluent NHibernate you would first need to create a model class. Consider the following database table. CREATE TABLE [dbo].[Product] ( [Id] INT NOT NULL PRIMARY KEY, [Name] VARCHAR(50) NULL, [Description] VARCHAR(50) NULL ) Here’s the corresponding model class. public class Product { public virtual int Id { get; set; } public virtual string Name { get; set; } public virtual string Description { get; set; } } Now that the database table and the corresponding model class is ready, the next step is to create the necessary mapping. To map an entity in Fluent NHibernate you should have a corresponding mapping class. Such mapping classes should derive from ClassMap where T represents the entity you are using. Fluent NHibernate uses strongly typed C# classes to map the properties of the model classes to the corresponding fields of the database tables. Here’s the mapping class named ProductMap. public class ProductMap : ClassMap<Product> { public ProductMap() { Id(x => x.Id); Map(x => x.Name); Map(x => x.Description); Table("Product"); } } The next step is to create a helper class to connect to our database. Here’s what this class would look like: public static class FluentNHibernateHelper { public static ISession OpenSession() { string connectionString = "Write your database connection string here"; ISessionFactory sessionFactory = Fluently.Configure() .Database(MsSqlConfiguration.MsSql2012 .ConnectionString(connectionString).ShowSql() ) .Mappings(m => m.FluentMappings .AddFromAssemblyOf<Product>()) .ExposeConfiguration(cfg => new SchemaExport(cfg) .Create(false, false)) .BuildSessionFactory(); return sessionFactory.OpenSession(); } } Note the call to the sessionFactory.OpenSession() in the last statement — this call actually creates a session of communication with the underlying database, i.e., it opens a connection to the database in use. You can now invoke the static method FluentNHibernateHelper.OpenSession() to open a connection to the database. The following code snippet illustrates how you can take advantage of the helper class created earlier to add a Product record to the Product database table. static void Main(string[] args) { using (var session = FluentNHibernateHelper.OpenSession()) { var product = new Product { Name = "Lenovo Laptop", Description = "Sample product" }; session.SaveOrUpdate(product); } } The following code snippet shows how you can query data from the database using our Fluent NHibernate helper class. using (ISession session = FluentNHibernateHelper.OpenSession()) { var products = session.Query<Product>().ToList(); //Usual code } To work with the code examples given in this article, you should ensure that the following namespaces have been added to your class. using FluentNHibernate.Cfg; using FluentNHibernate.Cfg.Db; using NHibernate; using NHibernate.Linq; using NHibernate.Tool.hbm2ddl; using System.Linq; You can learn more on working with Fluent NHibernate from GitHub. Related content feature What is Rust? Safe, fast, and easy software development Unlike most programming languages, Rust doesn't make you choose between speed, safety, and ease of use. Find out how Rust delivers better code with fewer compromises, and a few downsides to consider before learning Rust. By Serdar Yegulalp Nov 20, 2024 11 mins Rust Programming Languages Software Development how-to Kotlin for Java developers: Classes and coroutines Kotlin was designed to bring more flexibility and flow to programming in the JVM. Here's an in-depth look at how Kotlin makes working with classes and objects easier and introduces coroutines to modernize concurrency. By Matthew Tyson Nov 20, 2024 9 mins Java Kotlin Programming Languages analysis Azure AI Foundry tools for changes in AI applications Microsoft’s launch of Azure AI Foundry at Ignite 2024 signals a welcome shift from chatbots to agents and to using AI for business process automation. By Simon Bisson Nov 20, 2024 7 mins Microsoft Azure Generative AI Development Tools news Microsoft unveils imaging APIs for Windows Copilot Runtime Generative AI-backed APIs will allow developers to build image super resolution, image segmentation, object erase, and OCR capabilities into Windows applications. By Paul Krill Nov 19, 2024 2 mins Generative AI APIs Development Libraries and Frameworks Resources Videos