Create Database with FluentNHibernate

1

Is it possible through FluentNHibernate to create the database just as tables are created? I have the following class:

public class Helper : IRepositorioHelper
{
    private static ISessionFactory _sessionFactory;

    private static ISessionFactory SessionFactory() 
    {
        _sessionFactory = Fluently.Configure()            
            .Database(MsSqlConfiguration.MsSql2012
            .ConnectionString(x => x
                .Server(".\Servidor")
                .Database("BaseDados")
                .Username("sa")
                .Password("****"))
            .ShowSql())                
            .Mappings(x => x.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly()))
            .ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(true, true))
            .BuildSessionFactory();

        return _sessionFactory;            
    }

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

    public void CriaDatabase()
    {
        SessionFactory();
    }
 } 

With it I can create the tables without problems but I wanted to know if it is also possible to create the base to make it even easier when I distribute my application.

    
asked by anonymous 16.09.2014 / 04:24

2 answers

1

Thanks for the help but it did not work = ( I've tried the following:

public class Helper : IRepositorioHelper
{
    private static ISessionFactory _sessionFactory;
    private static FluentConfiguration Config
    {
        get
        {
            return Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2012
                    .ConnectionString(x => x
                        .Server(".\SQLExpress")
                        .Database("Alugo2")
                        .Username("sa")
                        .Password("sA@sqlserver"))
                        .ShowSql())                        
                    .Mappings(x => x.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly()));                        
        }
    }

    private static ISessionFactory SessionFactory(bool create = false) 
    {
        if(create)
        {
            Config.ExposeConfiguration(cfg => new SchemaExport(cfg).Execute(true, true, false));               
            //_sessionFactory = Config.BuildSessionFactory();
            Config.BuildConfiguration();
        }

        else
        {
            Config.ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(true, true));
            _sessionFactory = Config.BuildSessionFactory();
        }

        return _sessionFactory;            
    }

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

    public void CriaDatabase()
    {
        SessionFactory(true);
    }
}

When I use '_sessionFactory = Config.BuildSessionFactory ();' generates the following error:

/*An invalid or incomplete configuration was used while creating a SessionFactory.    Check   PotentialReasons collection, and InnerException for more detail.
em FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory()
em Alugo.Repositorio.NHibernate.Helper.SessionFactory(Boolean create) na d:\Arquivos \Documentos\Visual Studio 2013\Projects\Fluent\Alugo.Repositorio.Nhibernate\Helper.cs:linha 38
em Alugo.Repositorio.NHibernate.Helper.CriaDatabase() na d:\Arquivos\Documentos\Visual Studio 2013\Projects\Fluent\Alugo.Repositorio.Nhibernate\Helper.cs:linha 58
em Alugo.WindowsForm.frmMain.frmMain_Load(Object sender, EventArgs e) na d:\Arquivos\Documentos\Visual Studio 2013\Projects\Fluent\Alugo.WindowsForm\frmMain.cs:linha 36*/

And when I use 'Config.BuildConfiguration ();' does not give any error but also does not generate the database

    
19.09.2014 / 04:38
0

The solution would be to use SchemaExport (). Execute ()

Test the code similar to the following:

public FluentConfiguration GetConfig()
{
    return Fluently.Configure()
        .Database(
              MySQLConfiguration.Standard.ConnectionString(
                    c => c.Server("...").Database("...").Username("...").Password("..."))
        )
        .Mappings(
              m => m.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly())
        );
}

public void Export(bool script, bool export, bool justDrop)
{
    GetConfig()
         .ExposeConfiguration(
              c => new SchemaExport(c).Execute(script, export, justDrop))
         .BuildConfiguration();
}

Source: link

    
16.09.2014 / 12:13