问题
I have in project simple connection to database:
public static string GetConnectionString()
{
return @"Data Source=.\SQLEXPRESS;AttachDbFilename=" + HttpContext.Current.Request.PhysicalApplicationPath + "Application.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
}
Now I try to implement NHibernate to project.
hibernate.cfg.xml here:
<?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>
Test class "Clients" object here:
public class Clients
{
private Guid id;
private string name;
private string phone;
private string fax;
private string email;
private string address;
private string contactPerson;
public virtual Guid Id
{
get { return id; }
set { id = value; }
}
public virtual string Phone
{
get { return phone; }
set { phone = value; }
}
public virtual string Fax
{
get { return fax; }
set { fax = value; }
}
public virtual string Email
{
get { return email; }
set { email = value; }
}
public virtual string Address
{
get { return address; }
set { address = value; }
}
public virtual string ContactPerson
{
get { return contactPerson; }
set { contactPerson = value; }
}
public virtual string Name
{
get { return name; }
set { name = value; }
}
}
Clients.hbm.xml here:
<?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>
I used it like this:
public static void Configure()
{
sessions = new Configuration().Configure().AddClass(typeof(Clients)).BuildSessionFactory();
}
public static void Insert(Clients pb)
{
using (ISession session = sessions.OpenSession())
using (ITransaction tx = session.BeginTransaction())
{
session.Save(pb);
tx.Commit();
}
}
On Configure() method appears an error: Keyword not supported.Parameter name: attachdbfilename How fix??))
回答1:
<?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>
Notice you are using the NHibernate.Driver.MySqlDataDriver driver_class and NHibernate.Dialect.MySQL5Dialect dialect, you need to change it to:
<?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>
I assumed you are using MSSQL 2008, change to NHibernate.Dialect.MsSql2005Dialect if you are using 2005
https://community.jboss.org/wiki/DatabasesSupportedByNHibernate?_sscc=t
回答2:
You are using the MySqlDataDriver as your connection.driver_class in your NH config. Set it to NHibernate.Driver.SqlClientDriver instead.
Likewise, set the dialect to NHibernate.Dialect.MsSql2008Dialect
回答3:
have you tried a connection string without User Instance
http://social.msdn.microsoft.com/Forums/en/sqldataaccess/thread/60889070-5c3c-4823-ae9f-1ed6e32b2ce8
example:
Server=.\SQLExpress;AttachDbFilename=c:\mydbfile.mdf;Database=dbname; Trusted_Connection=Yes;
source: http://www.connectionstrings.com/sql-server-2005
To use the User Instance functionality you need to enable it on the SQL Server. This is done by executing the following command: sp_configure 'user instances enabled', '1'.
since the path may be absolute or relative, you can also try:
Data Source=.\SQLEXPRESS;AttachDbFilename=Application.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True
or
Data Source=.\SQLEXPRESS;AttachDbFilename=Application.mdf;Integrated Security=True;Connect Timeout=30
来源:https://stackoverflow.com/questions/9495525/how-connect-nhibernate-to-local-sqlexpress