无法在单声道命令行应用程序中使用 castle activerecord 吗?
我正在玩Castle activeRecord,并且我已经成功制作了一个asp.net MVC应用程序,它可以生成模式,执行增删改查操作并查询mysql数据库。我已经在 MonoDevelop 中完成了这个。
奇怪的是,如果我在命令行项目中使用几乎完全相同的代码,它就不起作用,并抛出以下异常:
Could not create the driver from NHibernate.Driver.MySqlDataDriver
抛出异常在下面代码中的 pc.Create();
处。尽管我告诉它生成架构,但这实际上从未发生,但它确实发生在 Web 应用程序中。
对此消息的一些谷歌搜索表明,当 mysql.data.dll 文件不在路径中或被引用等时,就会发生这种情况。它被引用,并且启用了“本地复制”,以便将其复制到调试文件夹中。我已经检查过它实际上也存在于文件夹中。
我加载了与 asp.net mvc 应用程序完全相同的引用。为什么会失败?
这是命令行应用程序的代码:
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
想通了。事实证明这是一个区分大小写的问题,并且与 castle AR 或 NHibernate 无关。
当我尝试在没有 activeRecord 的情况下使用 MySql.Data 程序集时,它给了我一个文件未找到异常,显示程序集名称为 MySql.Data.dll。我将 dll 的名称从 mysql.data.dll 更改为 MySql.Data.dll 并且它起作用了。
为什么程序集继续在全部小写的 ASP.NET MVC 应用程序中工作对我来说是一个谜。
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.