1

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;
}
Ryan Lundy
  • 204,559
  • 37
  • 180
  • 211
FernandoPaiva
  • 4,410
  • 13
  • 59
  • 118

2 Answers2

4

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.

Community
  • 1
  • 1
Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193
  • I created a project in Java with Hibernate to create the database first, and after it I used NHibernate to create tables, it work for me. I would use only Nhibernate but I can't find any solution to it. Thanks a lot. – FernandoPaiva Mar 22 '16 at 04:25
3

One full implementation could be:

For one entity class like Genre there will be a mapping class like GenreMap. And a general nhibernate helper class to get a session to db(SessionFactory) class for mapping all entities to database:

public class Genre
{
    public virtual int GenreId { get; set; }
    public virtual string Name { get; set; }
    public virtual string Description { get; set; }
}

public class GenreMap : ClassMap<Genre>
{
    public GenreMap()
    {
        Id(x => x.GenreId).Column("Id");
        Map(x => x.Name);
        Map(x => x.Description);
        Table("Genres");
    }
}

and finally in SessionFactory class:

public static class SessionFactory
{
    private static ISessionFactory _factory;

    public static ISession OpenSession()
    {
        return _factory.OpenSession();
    }

    public static void Init(string connectionString)
    {
        _factory = BuildSessionFactory(connectionString);
    }

    private static ISessionFactory BuildSessionFactory(string connectionString)
    {
        ISessionFactory sessionFactory = Fluently
            .Configure()
            .Database(PostgreSQLConfiguration.PostgreSQL81
                .ConnectionString(c => c.Is(connectionString))
                .ShowSql())

            .Mappings(m => m.FluentMappings.AddFromAssemblyOf<GenreMap>())
            .ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true))
            .BuildSessionFactory();

        return sessionFactory.OpenSession();
    }
}

And the usage will be like:

public ActionResult Index()
{
    using (ISession session = SessionFactory.OpenSession())
    {
        var persons = session.Query<Genre>().ToList();

        return View(persons);
    }
}

And in Startup class:

SessionFactory.Init(connectionString);
Daniel B
  • 3,109
  • 2
  • 33
  • 42
ILYES
  • 39
  • 6