c# - NHibernate AliasToBean 转换器抛出异常,然后 QueryOver 别名是私有(private)字段

标签 c# nhibernate visual-studio-2015 queryover resulttransformer

我在 VS2015 的 .net 4.5 目标项目中使用 NuGet 的 NHibernate 3.3.1.4000。

我有两个环境,第一个env1是带有VS2012和VS2015的Windows 8.1, 第二个 env2 使用 Windows 10,仅使用 VS2015。

QueryOver docs 中所述,别名可以是:

In QueryOver, aliases are assigned using an empty variable. The variable can be declared anywhere (but should be null at runtime). The compiler can then check the syntax against the variable is used correctly, but at runtime the variable is not evaluated (it's just used as a placeholder for the alias).

Each Lambda Expression function in QueryOver has a corresponding overload to allow use of aliases, and a .JoinAlias function to traverse associations using aliases without creating a sub-QueryOver.

因此,这意味着具有空值的私有(private)字段应该可以很好地用作别名。

但是......并不那么容易。

我有下一个例子:

    public void Test()
    {
        TestDto testDtoAlias = null;

        var users = GetSession()
            .QueryOver(() => _userAlias)
            .SelectList(list => list
                .Select(() => _userAlias.Id).WithAlias(() => testDtoAlias.UserId)
            )
            .TransformUsing(Transformers.AliasToBean<TestDto>())
            .List<TestDto>();
    }

    private class TestDto
    {
        public long UserId { get; set; }
    }

    private readonly User _userAlias = null;

env1 上工作得很好,但会抛出异常:

NHibernate.QueryException: could not resolve property: &lt;&gt;4__this._userAlias.Id of: User

env2上。

注意如果我选择不带 AleasToBean 的 Id 列表:

var users = GetSession()
            .QueryOver(() => _userAlias)
            .SelectList(list => list
                .Select(() => _userAlias.Id)
            )
            .List<long>();

它在两种环境下都能按预期工作。

对于可能导致 env2 出现问题的原因有什么想法吗?

工作很简单,只需在与方法相同的范围内创建别名,但我想知道我在配置中缺少什么,因为该示例似乎是正确的。 :(

最佳答案

经过一番苦苦寻找,我找到了答案。 NHibernate 有一些 conflicts with "Roslyn" compiler ,但他们在下一个版本中修复了它们 4.0.4, 3.4.1, and 3.3.5 。 只需将 NHibernate 更新到此版本之一,就解决了我的问题。

关于c# - NHibernate AliasToBean 转换器抛出异常,然后 QueryOver 别名是私有(private)字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37261674/

相关文章:

c# - 在 C# SDK 中使用存储连接字符串通过 URI 访问 blob

NHibernate 不允许我在 session 中插入模型,如果它是该 session 失败事务的一部分

asp.net-mvc - Asp.net mvc 网页在构建后不会加载

NHibernate:子查询.Exists 不起作用

c++ - 结构内的函数原型(prototype) - Intellisense 找不到

.net - .NET Core 项目的 C++ 项目依赖项

c# - 从程序文件访问时文件访问被拒绝

c# - 如何在 ASP.NET 中处理微前端?

c# - 如何在 Azure 中执行区分大小写的 LINQ 查询?

nhibernate - nHibernate限制包含