Unity and FluentNHibernate possible?

Hey everyone,

I want to be able to access data in an database from within my unity game. After looking for ways to access databases in c#/.net I found FluentNHibernate which looks really promising to me. I downloaded the version for .net 3.5 and added all the dlls to my unity project and created a NHibernateHelper class and a simple Entity and EntityMapping. When I try to call OpenSession on the NHibernateHelper class I get an exception.

NHibernateHelper class

class NHibernateHelper
    {
        //These are filled in correctly and a connection to the database is possible from the machine
        private const string ip = "xxx.xxx.xxx.xxx";
        private const string db = "dbname";
        private const string user = "username";
        private const string pw = "password";

        public NHibernateHelper()
        {
            InitializeSessionFactory();
        }

        private static ISessionFactory sessionFactory;

        private static ISessionFactory SessionFactory
        {
            get
            {
                if(sessionFactory == null)
                {
                    InitializeSessionFactory();
                }

                return sessionFactory;
            }
        }

        private static void InitializeSessionFactory()
        {
            sessionFactory = Fluently.Configure()
                .Database(
                    MySQLConfiguration.Standard.ConnectionString(
                        cs => cs.Server(ip)
                                .Database(db)
                                .Username(user)
                                .Password(pw)
                    )
                )
                .Mappings(
                    m => m.FluentMappings.AddFromAssemblyOf<NHibernateHelper>()
                )
                //.ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true))
                .BuildSessionFactory();
        }

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

SimpleEntity

public class SimpleEntity
    {
        public virtual int Id { get; set; }
        public virtual string name { get; set; }
        public virtual string description { get; set; }
    }

SimpleMapping

class SimpleMapping : ClassMap<SimpleEntity>
    {
        public SimpleMapping()
        {
            this.Id(x => x.Id);
            this.Map(x => x.name);
            this.Map(x => x.description);
        }
    }

Trying to update the database

void Start()
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    SimpleEntity simpleEntity = new SimpleEntity { name = "Test", description = ""};
                    session.Save(simpleEntity);
                    transaction.Commit();
                }
            }
        }

Error thrown by unity

ArgumentNullException: Argument cannot be null.
Parameter name: path1
System.IO.Path.Combine (System.String path1, System.String path2) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.IO/Path.cs:112)
NHibernate.LoggerProvider.GetNhibernateLoggerClass ()
NHibernate.LoggerProvider..cctor ()
Rethrow as TypeInitializationException: An exception was thrown by the type initializer for NHibernate.LoggerProvider
NHibernate.Cfg.Configuration..cctor ()
Rethrow as TypeInitializationException: An exception was thrown by the type initializer for NHibernate.Cfg.Configuration
FluentNHibernate.Cfg.FluentConfiguration..ctor ()
FluentNHibernate.Cfg.Fluently.Configure ()
NHibernateHelper.InitializeSessionFactory () (at Assets/Source/Scripts/Persistence/NHibernateHelper.cs:36)
NHibernateHelper.get_SessionFactory () (at Assets/Source/Scripts/Persistence/NHibernateHelper.cs:27)
NHibernateHelper.OpenSession () (at Assets/Source/Scripts/Persistence/NHibernateHelper.cs:55)
PersistenceTest.Start () (at Assets/Source/Scripts/Persistence/PersistenceTest.cs:43)

I tried to look for tutorials on how to integrate FluentNHiberante with unity but was unable to find one. I also tried to look for the error but was also unable to find anything helpful.
So my Question is has anyone successfully integrated FluentNHiberante with unity before and how? Or is it not possible due to issues with mono versions or something along those lines?
Thanks for any help in advance I really appreciate it.

Using:
Unity 5.0.0f3 on MacOSX
FluentNHibernate for .NET 3.5 version 1.5.1.0 (lastest version on NuGet)

Hi.
I have the same problem on Win7 64bit. Any ideas?

Hello lechmikos,

After doing a lot more research and trial and error I got NHibernate working inside of Unity3d. First you need to download the source of NHibernate 3.x.x, I used 3.4.0. Then in the Logging.cs in line 85 you need to change baseDir to equal System.Environment.CurrentDirectory. After that compile the project and put the dlls in the Assets folder of your project and lastly you need to add the .net connector for the database you are using, MySQL.Data.dll in my case. After that everything worked fine for me inside the Editor, Win and Mac builds. Hope this works for you too.

2 Likes

Thanks JonyderKiller! It also works for me. :slight_smile: