I'm looking for any way to create database
and tables
with NHibernate and FluentNHibernate.
How could I do this ?
trying.
private static ISessionFactory createConnection()
{
if (session != null)
return session;
//database configs
FluentConfiguration _config = Fluently.Configure().Database(
MySQLConfiguration.Standard.ConnectionString(
x => x.Server(HOST).
Username(USER).
Password(PASSWORD).
Database(DB)
))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<PerfilMap>())
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<ModuloMap>())
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<PermissaoMap>())
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<UsuarioMap>())
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<CategoriaMap>())
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<SubcategoriaMap> ())
.ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(false, true));
session = _config.BuildSessionFactory();
return session;
}
Neither NHibernate nor FNH create the database itself. You have to provide either code or a precreated database yourself. But it can create the tables for you. The class doing this is call SchemaExport
. In Fluent, it looks like this:
var sessionFactory = Fluently.Configure()
.Database(/* ... */)
.Mappings(/* ... */)
.ExposeConfiguration(cfg => new SchemaExport(cfg).Execute(true, true, false))
.BuildSessionFactory();
Shamelessly copied from this SO question.
I will give you an example :
You create 3 classes : Genre.cs , GenreMap.cs and NHibernateHelper.cs
In Genre.cs you find :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace TelerikMvcApp1.Models
{
public class Genre
{
public virtual int GenreId { get; set; }
public virtual string Name { get; set; }
public virtual string Description { get; set; }
}
}
In GenreMap.cs you find :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FluentNHibernate.Mapping;
namespace TelerikMvcApp1.Models
{
public class GenreMap : ClassMap <Genre>
{
public GenreMap()
{
Id(x => x.GenreId).Column("Id");
Map(x => x.Name);
Map(x => x.Description);
Table("Genres");
}
}
}
and In NHibernateHelper.cs you find :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FluentNHibernate;
using NHibernate;
using Npgsql;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate.Tool.hbm2ddl;
namespace TelerikMvcApp1.Models
{
public class NHibernateHelper
{
public static ISession OpenSession()
{
ISessionFactory sessionFactory = Fluently
.Configure()
.Database(PostgreSQLConfiguration.PostgreSQL81
.ConnectionString(c => c.Is("Server=localhost;Port=5432;Database=/*your db name*/;User Id=/*your id*/;Password= /*your passwoed*/;"))
.ShowSql())
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<GenreMap>())
.ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true))
.BuildSessionFactory();
return sessionFactory.OpenSession();
}
}
}
AND DON'T FORGET TO SPECIFY YOUR CORRECT DATA BASE ID AND PASSWORD
Finally , you can go to your controller and put your Index method like this :
public ActionResult Index()
{
using (ISession session = NHibernateHelper.OpenSession())
{
var persons = session.Query<Genre>().ToList();
return View(persons);
}
}