我正在使用Castle activeRecord,我已经成功制作了一个asp.net MVC应用程序,它可以生成模式,执行crud操作并查询mysql数据库。我在MonoDevelop中做过这个。
令人讨厌的是,如果我在命令行项目中使用几乎完全相同的代码,它就不起作用,抛出以下异常:
Could not create the driver from NHibernate.Driver.MySqlDataDriver
在下面的代码中的pc.Create();
处抛出异常。即使我告诉它生成模式,这实际上从未发生过,但它确实发生在Web应用程序中。
这条消息的一些谷歌搜索表明当mysql.data.dll文件不在路径或引用等时会发生这种情况。它被引用,并且“本地复制”被启用,因此它被复制到调试文件夹中。我已经检查过它实际上也存在于文件夹中。
我有与asp.net mvc app完全相同的引用。为什么会失败?
以下是命令行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; }
}
}
这是堆栈跟踪:
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
答案 0 :(得分:0)
想出来。事实证明这是一个区分大小写的问题,它与城堡AR或NHibernate没什么关系。
当我尝试使用没有activeRecord的MySql.Data程序集时,它给了我一个未找到文件的异常,显示程序集名称为MySql.Data.dll。我将dll的名称从mysql.data.dll更改为MySql.Data.dll,并且有效。
为什么程序集继续在asp.net mvc应用程序中运行,所有小写对我来说都是个谜。