mvc4 整合nhibernate3.0配置

來源:互聯網
上載者:User

標籤:

首先介紹下我的開發環境是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配置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.