简体   繁体   中英

NHibernate QueryOver Alias Issue

I'm using the latest version of NHibernate (3.3.1.4000) from NuGet in .Net 4 targeted project in Visual Web Developer 2010 Express.

When I attempt to follow examples I've seen for defining aliases, I get an exception when setting them up using lambdas (see screenshot).

显示错误'无法将lambda表达式转换为键入'string'...

As you can see I'm getting the error Cannot convert lambda expression to type 'string' because it is not a delegate type .

I have references to the LINQ namespaces in the top of my code:

using System.Linq;
using System.Linq.Expressions;

Any thoughts on what might be causing the problem?

In order to use a variable like role in an expression, you have to define it first, like so...

Role roleAlias = null; // <-- these two lines are missing from your code.
Person personAlias = null; 

var x = session.QueryOver<Role>(() => roleAlias)
    .JoinAlias(r => r.People, () => personAlias)
    // ...

ISession.QueryOver<T>(...) has four overloads:

  • .QueryOver<T>()
  • .QueryOver<T>(Expression<Func<T>> alias)
  • .QueryOver<T>(string entityName)
  • .QueryOver<T>(string entityName, Expression<Func<T>> alias)

Apparently because it can't figure out what role is, it's assuming you're trying to use the .QueryOver<T>(string entityName) overload, hence the "Cannot convert ... to type 'string'" error message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM