zoukankan      html  css  js  c++  java
  • 关于NHibernate示例程序QuickStart无法运行的问题

    今天看了下NHibernate的示例程序(版本2.1.0Beta2),按照帮助文档的步骤示例程序无法执行。稍加修改后才可以运行,具体原因总结如下:

    • 增加配置项

    <session-factory>

                  <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>

                  <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>

                  <property name="connection.connection_string">Server=(local);initial catalog=quickstart;Integrated Security=SSPI</property>

                  <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>

                  <mapping assembly="QuickStart"/>

    </session-factory>

     

    注:黄色部分需要添加

    如果没有添加,可能出现错误:"The ProxyFactoryFactory was not configured."

    • 拷贝相关联DLL到bin目录

    NHibernate.ByteCode.Castle.dll

    Castle.Core.dll

    Castle.DynamicProxy2.dll

    如果缺失相关程序集DLL,可能出现错误:"NHibernate.ByteCode.Castle.ProxyFactory”的类型初始值设定项引发异常"

    • 在Visual Studio中右击Cat.hbm.xml文件,设置属性:

    编译动作(Build Action)->嵌入资源(Embedded Resource)

    如果没有设置,可能出现的错误:No persister for QuickStart.Cat...

    • 注意每次数据库操作,都需使用事务来操作

    ITransaction tx = session.BeginTransaction();

    ....

    ....

    tx.Commit();
    如果没有使用事务,类实例持久化操作不会写入数据库

     补充NHibernate的使用步骤:

    1. 在app.config或web.config文件中增加NHibernate配置信息:configSection和hibernate-configuration节

    2. 添加对NHibernate.dll程序集的引用

    3. 编写持久化类,将需要持久化的属性设置为virtual

    4. 编写Class.hbm.xml映射关系文件

    5. 使用ISession和ISessionFactory接口进行持久化操作

  • 相关阅读:
    pythonchallenge 解谜 Level 6
    pythonchallenge 解谜 Level 5
    pythonchallenge 解谜 Level 4
    pythonchallenge 解谜 Level 3
    pythonchallenge 解谜 Level 2
    pythonchallenge 解谜 Level 1
    CCF 201912-2 回收站选址
    JavaWeb+JDBC+Servlet+SqlServer实现登录功能
    后台连接数据库的方法
    jQuery实现轮播图
  • 原文地址:https://www.cnblogs.com/andy65007/p/1514366.html
Copyright © 2011-2022 走看看