Nhibernate 2.1和mysql 5 - 安装程序上的InvalidCastException

时间:2009-06-14 08:15:20

标签: c# mysql nhibernate

我正在尝试将NHibernate与Spring.Net和mySQL 5一起使用。但是,在设置连接并创建SessionFactoryObject时,我得到了这个InvalidCastException:

NHibernate似乎将MySql.Data.MySqlClient.MySqlConnection转换为System.Data.Common.DbConnection,这会导致异常。

System.InvalidCastException wurde nicht behandelt.
  Message="Das Objekt des Typs \"MySql.Data.MySqlClient.MySqlConnection\" kann nicht in Typ \"System.Data.Common.DbConnection\" umgewandelt werden."
  Source="NHibernate"
  StackTrace:
       bei NHibernate.Tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.Prepare() in c:\CSharp\NH\nhibernate\src\NHibernate\Tool\hbm2ddl\SuppliedConnectionProviderConnectionHelper.cs:Zeile 25.
       bei NHibernate.Tool.hbm2ddl.SchemaMetadataUpdater.GetReservedWords(Dialect dialect, IConnectionHelper connectionHelper) in c:\CSharp\NH\nhibernate\src\NHibernate\Tool\hbm2ddl\SchemaMetadataUpdater.cs:Zeile 43.
       bei NHibernate.Tool.hbm2ddl.SchemaMetadataUpdater.Update(ISessionFactory sessionFactory) in c:\CSharp\NH\nhibernate\src\NHibernate\Tool\hbm2ddl\SchemaMetadataUpdater.cs:Zeile 17.
       bei NHibernate.Impl.SessionFactoryImpl..ctor(Configuration cfg, IMapping mapping, Settings settings, EventListeners listeners) in c:\CSharp\NH\nhibernate\src\NHibernate\Impl\SessionFactoryImpl.cs:Zeile 169.
       bei NHibernate.Cfg.Configuration.BuildSessionFactory() in c:\CSharp\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs:Zeile 1090.
       bei OrmTest.Program.Main(String[] args) in C:\Users\Max\Documents\Visual Studio 2008\Projects\OrmTest\OrmTest\Program.cs:Zeile 24.
       bei System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       bei System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       bei Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       bei System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       bei System.Threading.ThreadHelper.ThreadStart()
  InnerException:

我正在使用以编程方式设置的方法来获得快速的NHibernate设置。这是设置代码:

            Configuration config = new Configuration();
            Dictionary props = new Dictionary();
            props.Add("dialect", "NHibernate.Dialect.MySQL5Dialect");
            props.Add("connection.provider", "NHibernate.Connection.DriverConnectionProvider");
            props.Add("connection.driver_class", "NHibernate.Driver.MySqlDataDriver");
            props.Add("connection.connection_string", "Server=localhost;Database=orm_test;User ID=root;Password=password");
            props.Add("proxyfactory.factory_class", "NHibernate.ByteCode.Spring.ProxyFactoryFactory, NHibernate.ByteCode.Spring");
            config.AddProperties(props);
            config.AddFile("Person.hbm.xml");
            ISessionFactory factory = config.BuildSessionFactory();
            ISession session = factory.OpenSession();

缺少什么?我从mysql网站下载了当前的mysql连接器。

1 个答案:

答案 0 :(得分:0)

对我感到羞耻,我意外地下载了一个错误的mysql连接器而不是最新版本:http://dev.mysql.com/downloads/connector/net/6.0.html

抱歉!