Introduction
- Creating the simple WebAPI project.
- Installing the NHibernate using the Package Manager console.
- Defining a simple business object.
- Creating an NHibernate Mapper class to load and save the business object.
- Configure the NHibernate to link with the database.
- Writing simple CRUD operations.
Create WebAPI Project
Create a simple web API project named NHibernate Example.
Create a Database
Create a database named NHibernateExample and add a table with the name “User”.
Create a Business Object
Create a class user as in the following:


- public class User
- {
- public virtual int Id { get; set; }
- public virtual String FirstName { get; set; }
- public virtual String LastName { get; set; }
- }
- public class UserMap: ClassMapping < User > {
- public UserMap() {
- Schema("[dbo]");
- Table("[User]");
- Id(i = > i.Id, map = > map.Generator(Generators.GuidComb));
- Property(x = > x.FirstName, m = > {
- m.NotNullable(true);
- });
- Property(x = > x.LastName, m = > {
- m.NotNullable(true);
- });
- }
- }
- public static class ConfigurationManager {
- public static Configuration SetConfiguration() {
- var mapper = new ModelMapper();
- var config = new Configuration();
- mapper.AddMappings(Assembly.GetExecutingAssembly().GetExportedTypes());
- config.DataBaseIntegration(x = > {
- x.ConnectionString = "Data Source=U6031005-TPL-A;Initial Catalog=NHibernateExample;Integrated Security=True";
- x.Driver < SqlClientDriver > ();
- x.Dialect < MsSql2008Dialect > ();
- });
- config.AddAssembly(Assembly.GetExecutingAssembly());
- config.AddMapping(mapper.CompileMappingForAllExplicitlyAddedEntities());
- return config;
- }
- }
- public class DalUser: IDALUser {
- private ISessionFactory sessionFactory;
- public DalUser() {
- var config = ConfigurationManager.SetConfiguration();
- sessionFactory = config.BuildSessionFactory();
- }
- public List < User > UserList() {
- var userList = new List < User > ();
- using(var session = sessionFactory.OpenSession()) {
- using(var tx = session.BeginTransaction()) {
- userList = session.Query < User > ().ToList();
- tx.Commit();
- }
- }
- return userList;
- }
- }
- public class BLUser: IBLUser {
- public List < User > UserList() {
- IDALUser dalUser = new DalUser();
- return dalUser.UserList();
- }
- }
- [RoutePrefix("User")]
- public class UserController: ApiController {
- [Route("UserList")]
- public IHttpActionResult getUserList() {
- IBLUser blUser = new BLUser();
- var userlist = blUser.UserList();
- return Ok(userlist);
- }
- }
- http://localhost /User/UserList
- <ArrayOfUser
- xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://schemas.datacontract.org/2004/07/TestNHibernate.Models">
- <User>
- <FirstName>Rakesh Kumar</FirstName>
- <Id>1</Id>
- <LastName>Kotha</LastName>
- </User>
- </ArrayOfUser>

Former memberPosted Jun 21, 2017, 6:21 AM
I am not familiar with NHibernate but what i read from your article i feel that many things we need to code by hand where when we work with EF then we have to write minimum things and easy to go with. thanks
Santhakumar MunuswamyPosted Jul 3, 2015, 2:45 PM
Welcome..:) Thanks for nice article.:)
Santhakumar MunuswamyPosted Jul 3, 2015, 2:44 PM
Good Start
RakeshPosted Jul 1, 2015, 12:13 AM
Nice one brother welcome to C# Corner
Gopi ChandPosted Jun 30, 2015, 11:20 PM
Great...welcome