使用 `NHibernate.Bytecode.DefaultProxyFactoryFactory` 的异常

Exception using `NHibernate.Bytecode.DefaultProxyFactoryFactory`

我正在尝试进行 MSTest 单元测试 运行ning,并且我在 App.config 的 NHibernate 配置部分中有以下行:

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

每当我尝试 运行 单元测试时,我都会在测试图的 ClassInitialize 方法中遇到异常。例外情况是:

NHibernate.Bytecode.UnableToLoadProxyFactoryFactoryException: Unable to load type 'NHibernate.ByteCode.DefaultProxyFactoryFactory, NHibernate.Bytecode' during configuration of proxy factory class.

我的初始化代码是:

private static ISessionFactory _sessionFactory;
private static Configuration _configuration;

[ClassInitialize()]
public static void ProductRepositoryInitialize(TestContext testContext)
{
    _configuration = new Configuration();
    _configuration.Configure();
    _configuration.AddAssembly(typeof(Product).Assembly);
    _sessionFactory = _configuration.BuildSessionFactory();
}

然而 DefaultProxyFactoryFactory 可用,因为我可以使用以下行在控制台应用程序中实例化:

NHibernate.Bytecode.DefaultProxyFactoryFactory factory = new DefaultProxyFactoryFactory();

那么为什么 NHibernate 在我的单元测试中无法实例化甚至找不到这种类型 class。也许测试 class 初始值设定项是静态的?

突发新闻: 如果我使用相同的代理工厂但配置流畅,我会感到幸福和快乐,并将行插入到我的数据存储中。

_factory = Fluently.Configure()
    .Database(MsSqlConfiguration.MsSql2012)
    .Mappings(m => m.HbmMappings.AddFromAssemblyOf<Product>())
    .ProxyFactoryFactory<DefaultProxyFactoryFactory>()
    .BuildSessionFactory();

我可能来自与 MsTest (Vs2012) 相关的问题 参见 Here

您也可以尝试 DeploymentItemAttribute

如果 NUnit 仍然失败,您可以检查目录中是否存在 all NHibernate dependencies

PascalCase 思维模式加上 IDE 自动完成背叛了你 ;)

错误:

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

正确:

<property name="proxyfactory.factory_class">NHibernate.Bytecode.DefaultProxyFactoryFactory, NHibernate</property>

Why Bytecode doesn't meet the format of the other Namespaces?

Because Bytcode is a full word not a composition of words so Pascal or Camel Case doesn't apply here.

无论如何,我不确定这一点,但我认为如果您在配置中省略 factory_class 属性,默认情况下会创建 DefaultProxyFactoryFactory。有人可以证实吗?