我在 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: <>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/