尝试创建 NHibernate 和 FluentNHibernate 来连接 MySQL

本文关键字:连接 MySQL FluentNHibernate 创建 NHibernate | 更新日期: 2023-09-27 18:36:23

我正在尝试使用NHibernate和FluentNHibernate创建与MySQL的连接。我添加了MySql.Data nuget但是当我尝试创建连接时,它会抛出异常并显示消息The name MySQLConfiguration not exist in the current context

我该如何解决这个问题?

正在尝试

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();
}

尝试创建 NHibernate 和 FluentNHibernate 来连接 MySQL

已解决。问题是我的Visual Studio 2012没有导入库,我不知道需要哪些库,它发生了问题。

现在它工作了。

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();
        }

    }
}