使用MySQL配置NHibernate出错,不和谐的解决方式

以前使用MySQL,只是简单的利用Connection,Command做一些小P测试程序,对MySQL的认识也只是停留在它是个数据库而已,跟SQL Server没有神马区别。

现在项目开发过程中,越来越依赖ORM、依赖注入,今天使用MySQL配置了一把,过程中出现了一点小小问题,主要原因就是:Spring不能初始化MySQL的数据库Provider。

我安装的MySQL版本是5.0.27

项目添加的MySQL驱动的版本是Assembly MySql.Data, Version 5.1.7.0

Spring相关配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net" xmlns:db="http://www.springframework.net/database">

<!-- Referenced by main application context configuration file -->
<description>
Definitions for the NHibernate Objects.
</description>

<!-- Database and NHibernate Configuration -->
<db:provider id="DbProvider"
provider
="MySql-5.0"
connectionString
="Data Source=localhost;User Id=root;Password=root;database=moye"/>

<object id="NHibernateSessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate12">
<property name="DbProvider" ref="DbProvider"/>
<property name="MappingAssemblies">
<list>
<value>Meego.Moye.HibernateDAL.Mapping</value>
</list>
</property>
<property name="HibernateProperties">
<dictionary>
<entry key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
<entry key="hibernate.dialect" value="NHibernate.Dialect.MySQL5Dialect"/>
<entry key="hibernate.connection.driver_class" value="NHibernate.Driver.MySqlDataDriver"/>
<entry key="hibernate.show_sql" value="false"/>
</dictionary>
</property>
</object>

<object id="HibernateTransactionManager"
type
="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate12">
<property name="DbProvider" ref="DbProvider"/>
<property name="SessionFactory" ref="NHibernateSessionFactory"/>
</object>

<object id="HibernateTemplate" type="Spring.Data.NHibernate.HibernateTemplate">
<property name="SessionFactory" ref="NHibernateSessionFactory" />
<property name="TemplateFlushMode" value="Auto" />
<property name="CacheQueries" value="true" />
</object>

</objects>

按照项目分层的原则,类关系如图:

注入TeacherDao:

<object id="TeacherDao" type="Meego.Moye.HibernateDAL.MySQL.TeacherDaoHibernate,Meego.Moye.HibernateDAL.MySQL">
<property name="HibernateTemplate" ref="HibernateTemplate"/>
</object>

在Default.aspx页面中注入TeacherDao:

<object type="~/Default.aspx">
<property name="TeacherDao" ref="TeacherDao"></property>
</object>

页面中获取List<Teacher>列表:

IList<Teacher> teachers = this.teacherDao.GetTeachers();

项目部署到现在,基本已经搞定,为了简单,这里没有配置IBLL层和BLLImpl层。

跟以前配置ORM及依赖注入一样,可是程序会奇怪的报一个错误:

Error thrown by a dependency of object 'MySql-5.0' defined in 'assembly [Spring.Data, Version=1.1.2.20125, Culture=neutral, PublicKeyToken=65e474d141e25e07], resource [Spring.Data.Common.dbproviders.xml]' : 
Unsatisfied dependency expressed through constructor argument with index 2 of type [System.Type] : 
Could not convert constructor argument value [MySql.Data.MySqlClient.MySqlConnection, MySql.Data, Version=5.0.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d] to required type [System.Type] : 
Cannot convert property value of type [System.String] to required type [System.Type] for property ''.
while resolving 'constructor argument with name dbmetadata' to '(inner object)' defined in 'assembly [Spring.Data, Version=1.1.2.20125, Culture=neutral, PublicKeyToken=65e474d141e25e07], resource [Spring.Data.Common.dbproviders.xml]'

主要原因是配置DbProvider时候,Spring不能初始化数据库Provider。

查看Spring支持数据库Provider:

名称

介绍

SqlServer-1.1

Microsoft SQL Server, provider V1.0.5.0 in framework .NET V1.1

SqlServer-2.0

Microsoft SQL Server, provider V2.0.0.0 in framework .NET V2.0

SqlServerCe-3.1

Microsoft SQL Server Compact Edition, provider V9.0.242.0

SqlServerCe-3.5.1

Microsoft SQL Server Compact Edition, provider V3.5.1.0

OleDb-1.1

provider V1.0.5000.0 in framework .NET V1.1

OleDb-2.0

provider V2.0.0.0 in framework .NET V2.0

OracleClient-2.0

Oracle, Microsoft provider V2.0.0.0

OracleODP-2.0

Oracle, Oracle provider V2.102.2.20

MySql

MySQL provider 1.0.10.1

MySql-1.0.9

MySQL provider 1.0.9

MySql-5.0

MySQL provider 5.0.7.0

MySql-5.0.8.1

MySQL provider 5.0.8.1

MySql-5.1

MySQL provider 5.1.2.2

MySql-5.1.4

MySQL provider 5.1.2.2

MySql-5.2.3

MySQL provider 5.2.3.0

Npgsql-1.0

Postgresql provider 1.0.0.0 (and 1.0.0.1 - were build with same version info)

Npgsql-2.0 -beta1

Postgresql provider 1.98.1.0 beta 1

Npgsql-2.0

Postgresql provider 2.0.0.0

DB2-9.0.0-1.1

IBM DB2 Data Provider 9.0.0 for .NET Framework 1.1

DB2-9.0.0-2.0

IBM DB2 Data Provider 9.0.0 for .NET Framework 2.0

DB2-9.1.0-1.1

IBM DB2 Data Provider 9.1.0 for .NET Framework 1.1

DB2-9.1.0.2

IBM DB2 Data Provider 9.1.0 for .NET Framework 2.

SQLite-1.0.43

SQLite provider 1.0.43 for .NET Framework 2.0

SQLite-1.0.47

SQLite provider 1.0.43 for .NET Framework 2.0

SybaseAse-12

Sybase ASE provider for ASE 12.x

SybaseAse-15

Sybase ASE provider for ASE 15.x

SybaseAse-AdoNet2

Sybase ADO.NET 2.0 provider for ASE 12.x and 15.x

Odbc-1.1

ODBC provider V1.0.5000.0 in framework .NET V1.1

Odbc-2.0

ODBC provider V2.0.0.0 in framework .NET V2

InterSystems.Data.CacheClient

Caché provider Version 2.0.0.1 in framework .NET V2


具体请查看Spring.Net 官方文档 Chapter 19. DbProvider

我的MySQL版本是5.1.7.0没有对应的版本,具体什么原因,在网站查找资料也没有发现解决方法,

没有办法我只好从MySQL的官方文档上下来了最新的MySQL-connector-net安装到机器上,重新启动网站,

测试已经没有问题。

难道是安装MySQL-connector-net时,重新配置了VS环境的问题?

本人就是爱折腾,接下来把MySQL-connector-net安装目录下的MySql.Data.dll拷贝出来,放到项目文件里,修改网站引用的程序集,

卸载MySQL-connector-net,测试网站,未果,还是报错。

最后修改DbProvider name属性:

<!-- Database and NHibernate Configuration -->
<db:provider id="DbProvider"
provider
="OracleClient-2.0"
connectionString
="Data Source=localhost;User Id=root;Password=root;database=moye"/>

测试正常。

有点无语,不过还是把mysql-connector-net安装上,算是一种合适的解决方式。

大家有没有遇到相同的问题,小弟对这其中的原理不是很清楚,希望知道的朋友帮忙解释一下,感谢。

posted @ 2011-04-10 17:29  XueM  Views(5480)  Comments(0Edit  收藏  举报