Question

I am playing with Castle activeRecord, and I have succesfully made an asp.net MVC app, that can generate the schema, do crud operations and query the mysql database. I have done this in MonoDevelop.

Werid thing is, if I use almost the exact same code in a commandline project, it doesn't work, throwing the following exception:

Could not create the driver from NHibernate.Driver.MySqlDataDriver

The exception is thrown at the pc.Create(); in the code below. Even though I tell it to generate the schema, this never actually happens, but it does happen in the web app.

Some googling of this message indicates that this happens when the mysql.data.dll file isn't in the path or referenced etc. It is referenced, and "local Copy" is enabled so it gets copied into the debug folder. I have checked that it is actually present in the folder too.

I have exactly the same references loaded as the asp.net mvc app. Why is it failing?

Here is the code of the command line app:

using System;
using Castle.ActiveRecord;
using Castle.ActiveRecord.Framework.Config;
using ArTestCmd;

namespace ArTestCmd
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            String configFile = @"/home/si/Projects/ArTestCmd/CastleAR.config";

            XmlConfigurationSource source = new XmlConfigurationSource(configFile);
            ActiveRecordStarter.Initialize(source, typeof(Pc));
            ActiveRecordStarter.UpdateSchema();

            Pc pc = new Pc();
            pc.name = "blah";
            pc.Create();

        }
    }

    [ActiveRecord]
    public class Pc : ActiveRecordBase<Pc>
    {
        [PrimaryKey]
        public int PcId { get; set; }
        [Property (NotNull=true,Unique=true)]
        public String name { get; set; }

    }
}

Here is the stack trace:

Unhandled Exception: NHibernate.HibernateException: Could not create the driver from NHibernate.Driver.MySqlDataDriver. ---> 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 
  at NHibernate.Driver.ReflectionBasedDriver..ctor (System.String providerInvariantName, System.String driverAssemblyName, System.String connectionTypeName, System.String commandTypeName) [0x00000] in <filename unknown>:0 
  at NHibernate.Driver.MySqlDataDriver..ctor () [0x00000] in <filename unknown>:0 
  at (wrapper managed-to-native) System.Reflection.MonoCMethod:InternalInvoke (System.Reflection.MonoCMethod,object,object[],System.Exception&)
  at System.Reflection.MonoCMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
  --- End of inner exception stack trace ---
  at System.Reflection.MonoCMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
  at System.Reflection.MonoCMethod.Invoke (BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
  at System.Reflection.ConstructorInfo.Invoke (System.Object[] parameters) [0x00000] in <filename unknown>:0 
  at System.Activator.CreateInstance (System.Type type, Boolean nonPublic) [0x00000] in <filename unknown>:0 
  at System.Activator.CreateInstance (System.Type type) [0x00000] in <filename unknown>:0 
  at NHibernate.Bytecode.ActivatorObjectsFactory.CreateInstance (System.Type type) [0x00000] in <filename unknown>:0 
  at NHibernate.Connection.ConnectionProvider.ConfigureDriver (IDictionary`2 settings) [0x00000] in <filename unknown>:0 
  --- End of inner exception stack trace ---
  at NHibernate.Connection.ConnectionProvider.ConfigureDriver (IDictionary`2 settings) [0x00000] in <filename unknown>:0 
  at NHibernate.Connection.ConnectionProvider.Configure (IDictionary`2 settings) [0x00000] in <filename unknown>:0 
  at NHibernate.Connection.ConnectionProviderFactory.NewConnectionProvider (IDictionary`2 settings) [0x00000] in <filename unknown>:0 
  at NHibernate.Cfg.SettingsFactory.BuildSettings (IDictionary`2 properties) [0x00000] in <filename unknown>:0 
  at NHibernate.Cfg.Configuration.BuildSettings () [0x00000] in <filename unknown>:0 
  at NHibernate.Cfg.Configuration.BuildSessionFactory () [0x00000] in <filename unknown>:0 
  at Castle.ActiveRecord.Framework.SessionFactoryHolder.GetSessionFactory (System.Type type) [0x00000] in <filename unknown>:0 
  at (wrapper remoting-invoke-with-check) Castle.ActiveRecord.Framework.SessionFactoryHolder:GetSessionFactory (System.Type)
  at Castle.ActiveRecord.Framework.SessionFactoryHolder.CreateSession (System.Type type) [0x00000] in <filename unknown>:0 
  at (wrapper synchronized) Castle.ActiveRecord.Framework.SessionFactoryHolder:CreateSession (System.Type)
  at Castle.ActiveRecord.ActiveRecordBase.InternalCreate (System.Object instance, Boolean flush) [0x00000] in <filename unknown>:0 
  at Castle.ActiveRecord.ActiveRecordBase.Create (System.Object instance) [0x00000] in <filename unknown>:0 
  at Castle.ActiveRecord.ActiveRecordBase.Create () [0x00000] in <filename unknown>:0 
  at IntervalTool.MainClass.schemaAction (System.String action, System.String filename) [0x00000] in <filename unknown>:0 
  at IntervalTool.MainClass.Main (System.String[] args) [0x00000] in <filename unknown>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: NHibernate.HibernateException: Could not create the driver from NHibernate.Driver.MySqlDataDriver. ---> 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 
  at NHibernate.Driver.ReflectionBasedDriver..ctor (System.String providerInvariantName, System.String driverAssemblyName, System.String connectionTypeName, System.String commandTypeName) [0x00000] in <filename unknown>:0 
  at NHibernate.Driver.MySqlDataDriver..ctor () [0x00000] in <filename unknown>:0 
  at (wrapper managed-to-native) System.Reflection.MonoCMethod:InternalInvoke (System.Reflection.MonoCMethod,object,object[],System.Exception&)
  at System.Reflection.MonoCMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
  --- End of inner exception stack trace ---
  at System.Reflection.MonoCMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
  at System.Reflection.MonoCMethod.Invoke (BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
  at System.Reflection.ConstructorInfo.Invoke (System.Object[] parameters) [0x00000] in <filename unknown>:0 
  at System.Activator.CreateInstance (System.Type type, Boolean nonPublic) [0x00000] in <filename unknown>:0 
  at System.Activator.CreateInstance (System.Type type) [0x00000] in <filename unknown>:0 
  at NHibernate.Bytecode.ActivatorObjectsFactory.CreateInstance (System.Type type) [0x00000] in <filename unknown>:0 
  at NHibernate.Connection.ConnectionProvider.ConfigureDriver (IDictionary`2 settings) [0x00000] in <filename unknown>:0 
  --- End of inner exception stack trace ---
  at NHibernate.Connection.ConnectionProvider.ConfigureDriver (IDictionary`2 settings) [0x00000] in <filename unknown>:0 
  at NHibernate.Connection.ConnectionProvider.Configure (IDictionary`2 settings) [0x00000] in <filename unknown>:0 
  at NHibernate.Connection.ConnectionProviderFactory.NewConnectionProvider (IDictionary`2 settings) [0x00000] in <filename unknown>:0 
  at NHibernate.Cfg.SettingsFactory.BuildSettings (IDictionary`2 properties) [0x00000] in <filename unknown>:0 
  at NHibernate.Cfg.Configuration.BuildSettings () [0x00000] in <filename unknown>:0 
  at NHibernate.Cfg.Configuration.BuildSessionFactory () [0x00000] in <filename unknown>:0 
  at Castle.ActiveRecord.Framework.SessionFactoryHolder.GetSessionFactory (System.Type type) [0x00000] in <filename unknown>:0 
  at (wrapper remoting-invoke-with-check) Castle.ActiveRecord.Framework.SessionFactoryHolder:GetSessionFactory (System.Type)
  at Castle.ActiveRecord.Framework.SessionFactoryHolder.CreateSession (System.Type type) [0x00000] in <filename unknown>:0 
  at (wrapper synchronized) Castle.ActiveRecord.Framework.SessionFactoryHolder:CreateSession (System.Type)
  at Castle.ActiveRecord.ActiveRecordBase.InternalCreate (System.Object instance, Boolean flush) [0x00000] in <filename unknown>:0 
  at Castle.ActiveRecord.ActiveRecordBase.Create (System.Object instance) [0x00000] in <filename unknown>:0 
  at Castle.ActiveRecord.ActiveRecordBase.Create () [0x00000] in <filename unknown>:0 
  at IntervalTool.MainClass.schemaAction (System.String action, System.String filename) [0x00000] in <filename unknown>:0 
  at IntervalTool.MainClass.Main (System.String[] args) [0x00000] in <filename unknown>:0 
Était-ce utile?

La solution

Figured it out. This turned out to be a case sensitive issue, and it was nothing really to do with castle AR or NHibernate.

When I tried using the MySql.Data assembly without activeRecord, it gave me a file not found exception, showing that the assembly name as MySql.Data.dll. I changed the name of the dll from mysql.data.dll to MySql.Data.dll and it worked.

Why the assembly continues to work in the asp.net mvc app with all lower case is a mystery to me.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top