Search code examples
fluent-nhibernatemono

Could not create the driver from NHibernate.Driver.MySqlDataDriver in Mono


I have created an small application, in which I'm using Fluent Nhibernate. Application run fine on windows but give the exception in Mono.

    ---> NHibernate.HibernateException: Could not create the driver from
NHibernate.Driver.MySqlDataDriver, NHibernate, Version=3.1.0.4000,
Culture=neutral, PublicKeyToken=aa95f207798dfdb4. --->
System.Reflection.TargetInvocationException: Exception has been thrown
by the target of an invocation. --->
System.Configuration.ConfigurationErrorsException: Failed to find or
load the registered .Net Framework Data Provider
'MySql.Data.MySqlClient'.
 at System.Data.Common.DbProviderFactories.GetFactory (System.String
providerInvariantName) [0x00000] in <filename unknown>:0 

I'm using Kubuntu 11.10 and Mono 2.10.5. Fluent NHibernate version is 1.2. Is Fluent NHibernate is not fully supported under Mono?


Solution

  • From my experience, Fluent NHibernate works fine on Mono.

    I actually just got this working yesterday. Unfortunately, I don't have my code in front of me, but I'll give you some tips until I can post working code...

    I assume you've downloaded the MySql.Data.dll and referenced it in your Mono project. Something to watch out for is that this file is usually called MySQL.Data.dll after download. It should be renamed to MySql.Data.dll (notice the case change on the 'Q' and 'L').

    The library then has to be registered in the GAC to work with Mono. Simply placing the file in a relative path and referencing it doesn't work (I'm not completely sure why). More info is here.

    gacutil /i MySql.Data.dll
    

    If it still doesn't work, you may need to write a driver that descends from NHibernate.Driver.ReflectionBasedDriver. It's a very small piece of code that I'll post later when I can get to it.

    Edit: Here's the MySQL driver I'm using.

    public class MySqlDriver : NHibernate.Driver.ReflectionBasedDriver
    {
        public MySqlDriver() : base(
            "MySql.Data, Version=6.4.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d",
            "MySql.Data.MySqlClient.MySqlConnection, MySql.Data, Version=6.4.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d",
            "MySql.Data.MySqlClient.MySqlCommand, MySql.Data, Version=6.4.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"
        ) { }
    
        public override bool UseNamedPrefixInParameter
        {
            get { return true; }
        }
    
        public override bool UseNamedPrefixInSql
        {
            get { return true; }
        }
    
        public override string NamedPrefix
        {
            get { return "@"; }
        }
    
        public override bool SupportsMultipleOpenReaders
        {
            get { return false; }
        }
    }
    

    ...and the session factory functions

    private string _ConnectionString = "";
    
    private ISessionFactory CreateSessionFactory()
    {
        return Fluently.Configure().Database(
            MySQLConfiguration.Standard.Driver<MySqlDriver>().ConnectionString(_ConnectionString)
        ).Mappings(
            m => m.FluentMappings.AddFromAssemblyOf<YourType>()
        ).ExposeConfiguration(
            BuildSchema
        ).BuildSessionFactory();
    }
    
    private void BuildSchema(Configuration config)
    {
        //new SchemaExport(config).Create(false, true);
        new SchemaUpdate(config).Execute(false, true);
    }