NHibernate AliasToBean transformer throws then the QueryOver alias is a private field

772 Views Asked by At

I'm using the NHibernate 3.3.1.4000 from NuGet in .net 4.5 targeted project in VS2015.

I have two environments, first env1 a windows 8.1 with VS2012 and VS2015, and second one env2 with windows 10 and only VS2015.

As is stated in QueryOver docs, aliases could be:

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.

So this means the private field with a null value should work just fine as an alias.

BUT ... is not so easy.

I have next example:

    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;

which works just fine on env1 but throws exception:

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

on env2.

Note if I select the list of Ids without AleasToBean:

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

it works as expected on both environments.

Any thoughts on what might be causing the problem on the env2?

The workarroud is easy, just create the aliasses on the same scope as the method, but I want to know what am I missing in configuration, since the example it seems correct. :(

2

There are 2 best solutions below

0
On BEST ANSWER

So after some hard search, I found the answer. NHibernate had some conflicts with "Roslyn" compiler, but they fixed them in next versions 4.0.4, 3.4.1, and 3.3.5. Just updating NHibernate to one of this version, fixed my problem.

0
On

See this answer https://stackoverflow.com/a/6894010/246811

The documentation may be a little vague but I believe it means variables that are scoped to a method or block.

Just use local variables.