Creating Mysql connection with Fluentnhibernate?

Asked

Viewed 601 times

1

I am trying to create a connection with Mysql using Nhibernate and Fluentnhibernate. I added by nuget the MySql.Data but when I try to make the connection MySQLConfiguration not found in context.

I’m trying like this.

private static ISessionFactory getConnection(){
    return Fluently.Configure().
            Database(MySQLConfiguration.Standard.ConnectionString(
            x=>x.Server("localhost").
               Username("root").
               Password("").
               Database("usuarios_db")
            )).
            Mappings(m => m.FluentMappings.AddFromAssemblyOf<UsuarioMap>()).
            ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(false, true)).
            BuildSessionFactory();
}

1 answer

1


Solved.

How did I not know which using were needed to make the connection got a little bit because my Visual Studio 2012 is not doing the using automatically, this is a problem that I haven’t been able to solve yet if someone knows how to solve and can help I appreciate.

here’s how it turned out.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NHibernate;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using ControleUsuarios.Map;
using NHibernate.Tool.hbm2ddl;


namespace ControleUsuarios.BD {

    public class BDConnect {

        private static ISessionFactory session;
        private static const String HOST = "localhost";
        private static const String USER = "root";
        private static const String PASSWORD = "";
        private static const String DB = "usuario_db";        

        /** create a connection with database */
        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<UsuarioMap>())
                                                                        .ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(false, true));

            session = _config.BuildSessionFactory();
            return session;
        }


        /** open a session to make transactions */
        public static ISession openSession() {
            return createConnection().OpenSession();
        }



    }
}

Browser other questions tagged

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