如何将nhibernate连接到本地SQLEXPRESS? [英] How connect nhibernate to local SQLEXPRESS?
问题描述
我在项目中具有与数据库的简单连接:
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";
}
现在,我尝试将NHibernate实现为项目.
Now I try to implement NHibernate to project.
hibernate.cfg.xml此处:
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:
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>
我这样使用它:
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();
}
}
在Configure()方法上出现错误:不支持关键字参数名称:attachdbfilename 如何解决?))
On Configure() method appears an error: Keyword not supported.Parameter name: attachdbfilename How fix??))
推荐答案
<?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.Driver.MySqlDataDriver driver_class和NHibernate.Dialect.MySQL5Dialect方言,您需要将其更改为:
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>
我假设您使用的是MSSQL 2008,如果使用的是2005,请更改为NHibernate.Dialect.MsSql2005Dialect
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
这篇关于如何将nhibernate连接到本地SQLEXPRESS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!