C# 如何将nhibernate连接到本地SQLEXPRESS?
我在项目中有到数据库的简单连接:C# 如何将nhibernate连接到本地SQLEXPRESS?,c#,nhibernate,nhibernate-mapping,C#,Nhibernate,Nhibernate Mapping,我在项目中有到数据库的简单连接: public static string GetConnectionString() { return @"Data Source=.\SQLEXPRESS;AttachDbFilename=" + HttpContext.Current.Request.PhysicalApplicationPath + "Application.mdf;Integrated Security=True;Connect Timeout=
public static string GetConnectionString()
{
return @"Data Source=.\SQLEXPRESS;AttachDbFilename=" + HttpContext.Current.Request.PhysicalApplicationPath + "Application.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
}
现在我尝试将NHibernate实现到项目中
hibernate.cfg.xml此处:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory,NHibernate.ByteCode.Castle</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
<property name="connection.connection_string">Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\1\Desktop\Application+\Application\Application.mdf;Integrated Security=True;User Instance=True</property>
<property name="dialect">NHibernate.Dialect.MySQL5Dialect</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="WrapperObjects" namespace="WrapperObjects">
<class name="Clients" table="Clients" lazy="false">
<id name="Id" column="id">
<generator class="native"></generator>
</id>
<property name="Name" column="name" type="System.String"/>
<property name="Phone" column="phone" type="System.String"/>
<property name="Fax" column="fax" type="System.String"/>
<property name="Email" column="email" type="System.String"/>
<property name="Address" column="address" type="System.String"/>
<property name="ContactPerson" column="contactPerson" type="System.String"/>
</class>
</hibernate-mapping>
Clients.hbm.xml此处:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory,NHibernate.ByteCode.Castle</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
<property name="connection.connection_string">Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\1\Desktop\Application+\Application\Application.mdf;Integrated Security=True;User Instance=True</property>
<property name="dialect">NHibernate.Dialect.MySQL5Dialect</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="WrapperObjects" namespace="WrapperObjects">
<class name="Clients" table="Clients" lazy="false">
<id name="Id" column="id">
<generator class="native"></generator>
</id>
<property name="Name" column="name" type="System.String"/>
<property name="Phone" column="phone" type="System.String"/>
<property name="Fax" column="fax" type="System.String"/>
<property name="Email" column="email" type="System.String"/>
<property name="Address" column="address" type="System.String"/>
<property name="ContactPerson" column="contactPerson" type="System.String"/>
</class>
</hibernate-mapping>
在Configure()方法上出现错误:关键字不受支持。参数名称:attachdbfilename
如何修复???)您是否尝试了没有
用户实例的连接字符串
例如:
Server=.\SQLExpress;AttachDbFilename=c:\mydbfile.mdf;Database=dbname; Trusted_Connection=Yes;
资料来源:
要使用用户实例功能,需要在SQL Server上启用它。这是通过执行以下命令完成的:sp_configure'user instances enabled','1'
由于路径可能是绝对路径或相对路径,您也可以尝试:
Data Source=.\SQLEXPRESS;AttachDbFilename=Application.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True
或
您正在NH配置中将MySqlDataDriver用作connection.driver_类。改为将其设置为NHibernate.Driver.SqlClientDriver
同样,将方言设置为NHibernate.dialogue.mssql2008dialogue
<?xml version="1.0" encoding="utf-8"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory,NHibernate.ByteCode.Castle</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
<property name="connection.connection_string">Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\1\Desktop\Application+\Application\Application.mdf;Integrated Security=True;User Instance=True</property>
<property name="dialect">NHibernate.Dialect.MySQL5Dialect</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
NHibernate.ByteCode.Castle.ProxyFactory,NHibernate.ByteCode.Castle
NHibernate.Connection.DriverConnectionProvider
NHibernate.Driver.MySqlDataDriver
数据源=。\SQLEXPRESS;AttachDbFilename=C:\Users\1\Desktop\Application+\Application\Application.mdf;综合安全=真实;用户实例=True
NHibernate.dialogue.mysql5dialogue
真的
请注意,您正在使用NHibernate.Driver.MySqlDataDriver_类和NHibernate.dialogue.mysql5dialogue方言,需要将其更改为:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory,NHibernate.ByteCode.Castle</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\1\Desktop\Application+\Application\Application.mdf;Integrated Security=True;User Instance=True</property>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
NHibernate.ByteCode.Castle.ProxyFactory,NHibernate.ByteCode.Castle
NHibernate.Connection.DriverConnectionProvider
NHibernate.Driver.SqlClientDriver
数据源=。\SQLEXPRESS;AttachDbFilename=C:\Users\1\Desktop\Application+\Application\Application.mdf;综合安全=真实;用户实例=True
NHibernate.dialogue.mssql2008dialogue
真的
我假设您使用的是MSSQL 2008,如果您使用的是2005,请更改为NHibernate.dialogue.mssql2005dialogue
<?xml version="1.0" encoding="utf-8"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory,NHibernate.ByteCode.Castle</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\1\Desktop\Application+\Application\Application.mdf;Integrated Security=True;User Instance=True</property>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>