NHibernate使用3中框架动态代理方式Nhibernate常见的错误

时间:2021-08-06 13:50:06

1.NHibernate使用3中框架动态代理方式
(1).Castle框架
如果使用Castle.DynamicProxy2动态代理,引用NHibernate.ByteCode.Castle.dll程序集并配置proxyfactory.factory_class
<property name="proxyfactory.factory_class"> NHibernate.ByteCode.Castle.ProxyFactoryFactory,NHibernate.ByteCode.Castle</property>
(2).LinFu框架
如果使用LinFu.DynamicProxy动态代理,引用NHibernate.ByteCode.LinFu.dll程序集并配置proxyfactory.factory_class节点为
<property name="proxyfactory.factory_class"> NHibernate.ByteCode.LinFu.ProxyFactoryFactory,NHibernate.ByteCode.LinFu</property>
(3).Spring.NET框架
引用NHibernate.ByteCode.Spring.dll程序集并配置proxyfactory.factory_class节点为
<property name="proxyfactory.factory_class"> NHibernate.ByteCode.Spring.ProxyFactoryFactory,NHibernate.ByteCode.Spring</property>

如果配置不正确可能导致异常:

Unable to load type 'NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu' during configuration of proxy factory class.
Possible causes are:
- The NHibernate.Bytecode provider assembly was not deployed.
- The typeName used to initialize the 'proxyfactory.factory_class' property of the session-factory section is not well formed.

Solution:
Confirm that your deployment folder contains one of the following assemblies:
NHibernate.ByteCode.LinFu.dll
NHibernate.ByteCode.Castle.dll

2.影射文件与数据库的字段配置不一致   异常:异常could not load an entity:.....................................

3.创建SessionFactory工厂时候

    private ISessionFactory GetSessionFactory()
        {
            Configuration confriguration = new Configuration().AddAssembly("DomainModel");           

             return   confriguration.Configure().BuildSessionFactory();//异常:Could not compile the mapping document: DomainModel.Customer.hbm.xml

        }

   这样是不会抛出异常的:

   private ISessionFactory GetSessionFactory()
        {
            Configuration confriguration = new Configuration().AddAssembly("DomainModel");
            return confriguration.BuildSessionFactory();
        }

4。影射文件的命名空间

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="DomainModel" namespace="DomainModel" default-lazy="false">

如果不加default-lazy="false"的话有可能抛出异常:“NHibernate.ByteCode.Castle.ProxyFactory”的类型初始值设定项引发异常。

5。至于加不加<mapping assembly="DomainModel"/>这个节点,在笔者的机子上是没有错误的。