mvc4 整合nhibernate3.0配置

首先介绍下我的开发环境是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;
            }
        }
    }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。