以该示例为基础示例,我创建了该应用程序,但是在执行该应用程序时出现以下错误。


未配置ProxyFactoryFactory。使用可用的NHibernate.ByteCode提供程序之一初始化会话工厂配置部分的'proxyfactory.factory_class'属性。示例:NHibernate.ByteCode.LinFu.ProxyFactoryFactory,NHibernate.ByteCode.LinFu示例:NHibernate.ByteCode.Castle.ProxyFactoryFactory,NHibernate.ByteCode.Castle


以下是我正在使用的代码段。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NHibernate;
using NHibernate.Cfg;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Configuration cfg = new Configuration();
        cfg.AddAssembly("NHibernate");

        ISessionFactory factory = cfg.BuildSessionFactory();
        ISession session = factory.OpenSession();
        ITransaction transaction = session.BeginTransaction();
        User newUser = new User();
        newUser.Id = "joe_cool";
        newUser.UserName = "Joseph Cool";
        newUser.Password = "abc123";
        newUser.EmailAddress = "[email protected]";
        newUser.LastLogon = DateTime.Now;

        // Tell NHibernate that this object should be saved
        session.Save(newUser);

        // commit all of the changes to the DB and close the ISession
        transaction.Commit();
        session.Close();
    }
}


我的app.config文件看起来像

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <section
          name="nhibernate"
          type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
        />
      </configSections>

      <nhibernate>
        <add
          key="hibernate.connection.provider"
          value="NHibernate.Connection.DriverConnectionProvider"
        />
        <add
          key="hibernate.dialect"
          value="NHibernate.Dialect.MsSql2000Dialect"
        />
        <add
          key="hibernate.connection.driver_class"
          value="NHibernate.Driver.SqlClientDriver"
        />
        <add
          key="hibernate.connection.connection_string"
          value="Server=localhost;initial catalog=nhibernate;Integrated Security=SSPI"
        />
        <!--<add value="nhibernate.bytecode.castle.proxyfactoryfactory, nhibernate.bytecode.castle" key="proxyfactory.factory_class" />-->
        <!--<property name="proxyfactory.factory_class">NHibernate.ByteCode.Linfu.ProxyFactoryFactory, NHibernate.ByteCode.Linfu</property>-->
<!-- I have tried both the lines but still getting the same error -->
      </nhibernate>
    </configuration>


我有LinFu.DynamicProxy.dll而不是linfu.dll。能行吗如果不是,那么我从哪里得到这个linfu.dll
还是有其他解决方案?

最佳答案

假设您拥有NHibernate 2.1 Alpha3,请将LinFu.DynamicProxy.dllNHibernate.ByteCode.LinFu.dll\Required_For_LazyLoading\LinFu复制到bin(或引用)中

然后,您的配置行应该工作:

<add key="proxyfactory.factory_class" value="NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu" />


顺便说一句,我更喜欢使用hibernate-configuration节块进行配置。

编辑:如果要设置hibernate-configuration而不是键/值对,则这是我的Web配置中的相关部分。

同样,也可以将hibernate-configuration部分放在其自己的名为hibernate.cfg.xml的文件中。然后,您可以使用下载中的xsd nhibernate-configuration.xsd来验证您的配置。

<configSections>
    <section name="hibernate-configuration"   type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/>
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
        <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
        <property name="default_schema">kennelfinder.dbo</property>
        <property name="connection.provider">
            NHibernate.Connection.DriverConnectionProvider
        </property>
        <property name="proxyfactory.factory_class">
            NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu
        </property>
        <property name="connection.connection_string">{Your connection string}</property>
        <property name="show_sql">false</property>
        <property name="connection.driver_class">
            NHibernate.Driver.SqlClientDriver
        </property>
        <property name="connection.isolation">ReadCommitted</property>
        <property name="use_proxy_validator">true</property>
        <mapping assembly="KennelFinder"/>
    </session-factory>
</hibernate-configuration>

08-27 11:03