標籤:
首先介紹下我的開發環境是vs2010旗艦版,nhibernate採用的是3.0版本。
一、在mvc4項目中裝載配置nhibernate的第一種方式是採用【hibernate.xml】的方式:首先看圖
使用這種方式配置nhibernate的話,需要將hibernate.xml【屬性】改為始終複製到輸入目錄或作為嵌入資源使用。
當然要使用nhibernate的化,需要在項目中引入hibernate.dll.
接下來我們來看下,在代碼中如何來裝載我們對nhibernate的配置:
public class NhibernateHelper { private ISessionFactory _sessionFactory; private static ISession _session = null; public NhibernateHelper() { _sessionFactory = GetSessionFactory(); } public ISessionFactory GetSessionFactory() { //負載檔案的方式 Configuration cfg = new Configuration().Configure(AppDomain.CurrentDomain.BaseDirectory+"/hibernate.cfg.xml"); //Configuration cfg = new Configuration(); //cfg.AddAssembly(Assembly.GetExecutingAssembly()); HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();//NHProf工具 cfg.AddAssembly("BusinessDemo");//BusinessDemo指的是包含資料表的dll類庫如:user.hbm.xml //cfg.AddAssembly(typeof(User).Assembly); //cfg.AddAssembly(typeof(Order).Assembly); return cfg.BuildSessionFactory(); } public ISession GetSession() { if (_session != null && _session.IsOpen) return _session; else { _session = _sessionFactory.OpenSession(); return _session; } } }
然後我們在mvc4 Controller裡邊就可以這樣來做下測試:
public class CustomerController : Controller{ public ISession session { get { return new NhibernateHelper().GetSession(); } } public ActionResult Index() { Customer customer = this.session.Load<Customer>(2); var result = new Result(); using (ITransaction tx = this.session.BeginTransaction()) { try { this.session.Delete(customer); this.session.Flush(); tx.Commit(); result.success = true; result.msg = "刪除成功!"; } catch (HibernateException) { tx.Rollback(); result.success = true; result.msg = "刪除失敗!"; } } return Json(result, JsonRequestBehavior.AllowGet); } }
二、接下來我們來看如何在麼mvc項目中如何在web.config中配置nhibernate資訊:直接上代碼
<configSections> <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" requirePermission="false"/> </configSections> <connectionStrings> </connectionStrings> <appSettings> <add key="webpages:Version" value="2.0.0.0" /> <add key="PreserveLoginUrl" value="true" /> <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> </appSettings> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> <session-factory> <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider, NHibernate</property> <property name="connection.connection_string">Database=test;Data Source=localhost;User Id=root;Password=root</property> <property name="dialect">NHibernate.Dialect.MySQL5Dialect</property> <property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property> <property name="proxyfactory.factory_class">NHibernate.Bytecode.DefaultProxyFactoryFactory,NHibernate</property> </session-factory> </hibernate-configuration>
註: <property name="dialect"> 、<property name="connection.driver_class">這兩個屬性一定要和連結的資料庫保持一致,這裡邊我採用的是mysql資料庫。
然後我們直接來看如何來裝載它:
public class NhibernateHelper { private ISessionFactory _sessionFactory; private static ISession _session = null; public NhibernateHelper() { _sessionFactory = GetSessionFactory(); } public ISessionFactory GetSessionFactory() { //負載檔案的方式 //Configuration cfg = new Configuration().Configure(AppDomain.CurrentDomain.BaseDirectory+"/hibernate.cfg.xml"); Configuration cfg = new Configuration(); cfg.AddAssembly(Assembly.GetExecutingAssembly()); HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();//NHProf工具 //cfg.AddAssembly("BusinessDemo"); //cfg.AddAssembly(typeof(User).Assembly); //cfg.AddAssembly(typeof(Order).Assembly); return cfg.BuildSessionFactory(); } public ISession GetSession() { if (_session != null && _session.IsOpen) return _session; else { _session = _sessionFactory.OpenSession(); return _session; } } }
mvc4 整合nhibernate3.0配置