Nhibernate常见的错误

本文详细介绍了NHibernate框架的三种动态代理配置方法,包括Castle、LinFu和Spring.NET框架,并探讨了配置不当可能引发的异常及解决策略。同时,文章还分析了映射文件与数据库字段配置不一致的问题,以及创建SessionFactory工厂时的注意事项。

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"/>这个节点,在笔者的机子上是没有错误的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值