如何将nhibernate连接到本地SQLEXPRESS?

时间:2012-02-29 08:06:01

标签: c# nhibernate nhibernate-mapping

我在项目中简单连接到数据库:

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>

测试类“客户端”对象在这里:

    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:

<?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 如何修复??))

3 个答案:

答案 0 :(得分:2)

<?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方言,您需要将其更改为:

<?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

https://community.jboss.org/wiki/DatabasesSupportedByNHibernate?_sscc=t

答案 1 :(得分:0)

你试过没有User Instance的连接字符串吗? http://social.msdn.microsoft.com/Forums/en/sqldataaccess/thread/60889070-5c3c-4823-ae9f-1ed6e32b2ce8

示例:

Server=.\SQLExpress;AttachDbFilename=c:\mydbfile.mdf;Database=dbname; Trusted_Connection=Yes;

来源:http://www.connectionstrings.com/sql-server-2005

要使用用户实例功能,您需要在SQL Server上启用它。这是通过执行以下命令来完成的:sp_configure'user instances enabled','1'。

由于路径可能是绝对路径或相对路径,您也可以尝试:

Data Source=.\SQLEXPRESS;AttachDbFilename=Application.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True

Data Source=.\SQLEXPRESS;AttachDbFilename=Application.mdf;Integrated Security=True;Connect Timeout=30

答案 2 :(得分:0)

您在NH配置中使用MySqlDataDriver作为connection.driver_class。改为将它设置为NHibernate.Driver.SqlClientDriver。

同样,将方言设置为NHibernate.Dialect.MsSql2008Dialect