I am trying to join aliases multiple times but it depends on the number of times it should do it.
Employee employeeAlias = null;
Thing thingAlias = null;
var names = string[] {"A", "B", "C", "D", "E"} // number of values could vary
for(int i = 0; i < names.Length ; i++)
{
queryOver
.JoinAlias(() => employeeAlias.Things,
() => thingAlias,
JoinType.LeftOuterJoin,
Restrictions.Eq(Projections.Property(() => thingAlias.Name,
names[i]));
}
However, upon execution, NHibernate throws an error saying:
NHibernate.QueryException: duplicate alias: thingAlias ---> System.ArgumentException: An item with the same key has already been added
Is there a way I can make NHibernate join an alias with a specified name so that it will not re-use the name of the variable?
Thanks guys. This has been my problem since yesterday and I can't find a proper solution.
PS:
I'm trying to do something like this:
SELECT * FROM Employee e
LEFT OUTER JOIN Thing t on e.Id = t.EmployeedId AND t.Name = "A"
LEFT OUTER JOIN Thing t on e.Id = t.EmployeedId AND t.Name = "B"
I didn't tried this but I think instead of using
for-loop
andRestrictions.Eq
, we can (not sure, but in case you didn't tried it) useRestrictions.In
like-What you are trying to achieve is impossible. Each relation (
many-to-one
,one-to-many
) can be used exactly once.To support this statement, please, check it here: CriteriaQueryTranslator.cs
So, what we can see here, regardless of aliases, at the end, all associtated relations (JOINS) are added into
associationPathCriteriaMap
, which isIDictionary<string, ICriteria>
.That means, that there is now way how to have two same keys in play...