主要是NHibernate连接MySQL注意事项:
- 在website的bin目录里添加MySql.Data.dll(可在官方下载)和hibernate.cfg.xml
- 启用新的OpenSession()方法
- 通过ADO连接数据库的方式会出现“用户名root无法登陆”的错误提示。所以全部通过NHibernate连接,使用NHibernate的函数。
- 在电脑上安装MySql的时候注意选择“Developer Machine”.要不然会出现无法启动MySql服务的错误:
- T-SQL和MySql一些常见的不兼容。
- top 10 -> limit index,length
- dbo. -> 空
- getDate() -> NOW()
- DATEADD(mi,15,GETDATE()) -> DATE_ADD(NOW(), INTERVAL 15 MINUTE)
- .....
参考:
- hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!--
This template was written to work with NHibernate.Test.
Copy the template to your NHibernate.Test project folder and rename it in hibernate.cfg.xml and change it
for your own use before compile tests in VisualStudio.
-->
<!-- This is the ByteFX.Data.dll provider for MySql -->
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.0" >
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
<property name="connection.connection_string">
Database=clientsystem_dbo;Data Source=127.0.0.1;User Id=root;Password=123456
</property>
<property name="dialect">NHibernate.Dialect.MySQLDialect</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
<property name="hbm2ddl.keywords">none</property>
</session-factory>
</hibernate-configuration>
- OpenSession() //MySql 初始化session配置
public static ISession OpenSession()
{
try
{
ISession session = null;
if (m_factory == null)
{
m_factory = m_cfg.Configure().Configure().AddAssembly(AssemblyClassName).BuildSessionFactory();
session = m_factory.OpenSession();
}
else
{
session = m_factory.OpenSession();
}
return session;
}
catch (Exception e)
{
return null;
throw e;
}
}