In the previous part we have installed APS.NET 5 application on Ubuntu, now we are gonna install and configure PostgreSQL on Ubuntu and access database from our application through NHibernate.

Install and configure PostgreSQL

First, install PostgreSQL:

sudo apt-get install postgresql postgresql-contrib

Next, set root password:

sudo -u postgres psql postgres
\password postgres

And then create test database:

sudo -u postgres createdb mydb

Add Fluent NHibernate to the application

Open project.json and add following dependencies:

"NHibernate": "",
"FluentNHibernate": "",
"Npgsql": ""

Create simple model:

public class Item
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }

and mapping for that model:

using FluentNHibernate.Mapping;

public class ItemMap : ClassMap<Item>
    public ItemMap()
        Id(item => item.Id).GeneratedBy.Increment();
        Map(item => item.Name);

Now you can add or delete items from database like this:

using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using System.Collections.Generic;

using System;
using System.Linq;

namespace DAL
    public class ItemContext
        public IList<Item> GetAll()
            var sessionFactory = CreateSessionFactory();

            using (var session = sessionFactory.OpenSession())
                using (var trans = session.BeginTransaction())
                    var item = new Item { Name = "Test"};

                using (session.BeginTransaction())
                    var items = session.CreateCriteria(typeof(Item)).List<Item>();
                    return items;

        private ISessionFactory CreateSessionFactory()
            return Fluently
                        .ConnectionString(c =>
                    .Mappings(m => m.FluentMappings.AddFromAssemblyOf<ItemMap>())
        private static void TreatConfiguration(Configuration configuration)
            // dump sql file for debug
            Action<string> updateExport = x =>
                using (var file = new System.IO.FileStream(@"update.sql", System.IO.FileMode.Append, System.IO.FileAcce$
                using (var sw = new System.IO.StreamWriter(file))
            var update = new SchemaUpdate(configuration);
            update.Execute(updateExport, true);

It will automatically create database scheme when you connect for the first time.

Add WebAPI controller to the application

Now it is time to test our data access layer, so we need to add some Web API methods for that.

Add ItemController.cs file to the Controllers folder:

using DAL;
using Microsoft.AspNet.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace HelloMvc.Controllers
    public class ItemController : Controller
        public IEnumerable<Item> GetAll()
            var allItems = new ItemContext().GetAll();
            return allItems;

Now start your application with:

k kestrel

and go to:

In the next part, I am going to show how to use ASP.NET 5 dependency injection and Fluent NHibernate auto-mapping with automatic migration.