Create Database with Fluentnhibernate

Asked

Viewed 272 times

1

Is it possible through Fluentnhibernate to create the database 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 tables without problems but I wanted to know if it is also possible to create the basis to make it even easier when distributing my application.

2 answers

1

Thanks for the help but it didn’t work =( I 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 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 using 'Config.Buildconfiguration();' does not give any error but also does not generate the database

0

The solution would be to use the Schemaexport(). Execute()

Test the code similar to this one below:

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: https://stackoverflow.com/a/7793133/194717

Browser other questions tagged

You are not signed in. Login or sign up in order to post.