關於如何在ASP.NET2.0網站與Visual Web Developer Express中配置NHibernate的問題

來源:互聯網
上載者:User

在ASP.NET項目中使用NHibernate老是會碰到無法載入設定檔的問題,剛才瞎折騰了1小時,才從外國的一篇部落格裡找到瞭解決方案.地址如下:http://derek-says.blogspot.com/2008/02/nhibernate-in-visual-web-developer.html.

 

現在我來簡單的講解下如何配置.

1.資料庫裡建表,如tb_Department

2.然後編寫對應的類檔案, 如我對應類的全名是:NetFuture.NHibernateClass.NH_tb_Department

3.在WebConfig中加入配置,一下是我的設定檔:

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
   <property name="connection.provider">
        NHibernate.Connection.DriverConnectionProvider
      </property>
   <property name="dialect">
        NHibernate.Dialect.MsSql2000Dialect
      </property>
   <property name="connection.driver_class">
        NHibernate.Driver.SqlClientDriver
      </property>
   <property name="connection.connection_string">
        Data Source=(local);Integrated Security=True; initial catalog=DB_PersonnelManageSystem
      </property>
   <mapping assembly="App_Code"/>
  </session-factory>
 </hibernate-configuration>

紅色的那句貌似是關鍵的地方.

4.編寫類對應的對應檔,如:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
  <class name="NetFuture.NHibernateClass.NH_tb_Department, App_Code" table="tb_Department" >
    <id name="DepartmentId" column="DepartmentId" type="Int32"  length="4" unsaved-value="0">
      <generator class="native" />
    </id>
    <property name="DepartmentName" column="DepartmentName" type="String" length="16" />
    <property name="Description" column="Description" type="String" length="200" />
    <property name="ManagerId" column="ManagerId" type="String" length="15" />
  </class>
</hibernate-mapping>
紅色的那句也是關鍵的地方.

5.將類與對應檔都放置與App_Code檔案夾下

6.編寫類似下面的NHibernate初始化配置代碼:

NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
                cfg.AddDirectory(new System.IO.DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath(@"~/App_Code/")));  

                _factory = cfg.BuildSessionFactory();

這樣就完成了對App_Code檔案夾的映射,NHibernate就會在其中搜尋設定檔了.

 

第一次寫隨筆,希望能對你有所協助^_^.

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.