chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
I get an exception while running this code:

var employee = session.Query<Employee>().Where(x => x.Name == "John").ToList();



This is the class Employee

public class Employee
    {
        public Guid UniqueId { get; set; }
        public string Name { get; set; }
    }



This is the Nhibernate mapping for this class

<xml>
<hibernate>
  <class>
    <id>
      <generator>
    </id>
    <property>
  </class>
</hibernate>


This is the exception thrown:

System.TypeInitializationException : The type initializer for 'NHibernate.Linq.NhRelinqQueryParser' threw an exception.
----> System.InvalidOperationException : Sequence contains more than one matching element
at NHibernate.Linq.NhRelinqQueryParser.Parse(Expression expression)
at NHibernate.Linq.NhLinqExpression.Translate(ISessionFactoryImplementor sessionFactory)
at NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(String queryIdentifier, IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary`2 filters, ISessionFactoryImplementor factory)
at NHibernate.Engine.Query.HQLExpressionQueryPlan.CreateTranslators(String expressionStr, IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory)
at NHibernate.Engine.Query.HQLExpressionQueryPlan..ctor(String expressionStr, IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory)
at NHibernate.Engine.Query.HQLExpressionQueryPlan..ctor(String expressionStr, IQueryExpression queryExpression, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory)
at NHibernate.Engine.Query.QueryPlanCache.GetHQLQueryPlan(IQueryExpression queryExpression, Boolean shallow, IDictionary`2 enabledFilters)
at NHibernate.Impl.AbstractSessionImpl.GetHQLQueryPlan(IQueryExpression queryExpression, Boolean shallow)
at NHibernate.Impl.AbstractSessionImpl.CreateQuery(IQueryExpression queryExpression)
at NHibernate.Linq.NhQueryProvider.PrepareQuery(Expression expression, ref IQuery query, ref NhLinqExpression nhQuery)
at NHibernate.Linq.NhQueryProvider.Execute(Expression expression)
at NHibernate.Linq.NhQueryProvider.Execute(Expression expression)
at Remotion.Data.Linq.QueryableBase`1.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList(IEnumerable`1 source)
at Fixture.Fixture.TestConfiguration() in Fixture.cs: line 28
--InvalidOperationException
at System.Linq.Enumerable.Single(IEnumerable`1 source, Func`2 predicate)
at Remotion.Data.Linq.Parsing.ExpressionTreeVisitors.Transformation.ExpressionTransformerRegistry.CreateDefault()
at NHibernate.Linq.NhRelinqQueryParser..cctor()


This is the inner exception:

Sequence contains more than one matching element
at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source, Func`2 predicate)
at Remotion.Data.Linq.Parsing.ExpressionTreeVisitors.Transformation.Expression TransformerRegistry.CreateDefault() in :line 0
at NHibernate.Linq.NhRelinqQueryParser..cctor() in d:CSharpNHNH hibernatesrcNHibernateLinqNhRelinqQueryParser.cs:line 26


If I disable TypeMock everything works fine.
Any tip? Thanks

Alex
asked by abellisomi (720 points)

7 Answers

0 votes
Additional clue... I verified that the problem disappear with the 3.2 beta version of Nhibernate (now we are using the 3.1 stable).
answered by abellisomi (720 points)
0 votes
Hi,

Thanks for the information.

I'd like to reproduce it and investigate this case. I'll update on this thread about it.

Regards,
Elisha,
Typemock Support
answered by Elisha (12k points)
0 votes
Thank you for help.
I could send you a simple project to replicate this, if you want it.

Regards
answered by abellisomi (720 points)
0 votes
Thanks,

It'll be very helpful, can you email it to support at typemock.com?

Regards,
Elisha,
Typemock Support
answered by Elisha (12k points)
0 votes
Hello,

I have just sent you the sample project. I hope it could help you finding this issue.

Alex
answered by abellisomi (720 points)
0 votes
I’ve discovered the cause, but as for an immediate fix, I’m not too sure.

Inside NHibernate is something called Remotion which is some library or other that does something that NH needs. They roll this dependency up inside NHibernate.dll using ILMerge (along with a load of other dependencies, hence the reason there’s only a few DLLs for NH and why it’s such behemoth).

Long story short, there’s a bug inside that library which tries to find out the framework version that is being used (3.5, 4.0, etc.) In order to do this in a low-trust environment, it runs the following code:

// typeof (object).Assembly.GetName() will throw an exception in medium trust.
// Therefore, scan references to detect the referenced framework version.
var mscorlibReference = Assembly.GetExecutingAssembly().GetReferencedAssemblies().Single (name => name.Name == "mscorlib");
var referencedFrameworkVersion = mscorlibReference.Version;

Oddly, the call to Single() to try to get the referenced version of mscorlib is not guaranteed to work. In some situations, there may well be two (or more?) versions of mscorlib referenced. This looks like what is happening inside the test when evaluating the expression, but only when Typemock is loaded. While Typemock and NHibernate both use mscorlib v2.0.0.0, it is probable that some sort of instrumentation caused by enabling Typemock throws mscorlib 4.0.0.0 into the mix.

This problem has been fixed in Re-motion v1.13.100.0, while NH v3.1 uses version 1.13.93.~

The beta NH v3.2 references Re-motion v1.13.100.1, so when it’s released this will be fixed…
answered by gurhall (2.3k points)
0 votes
Many many thanks to you abellisomi, I am also facing this problem and it's really helpful posting.
answered by kushal_fpost (220 points)
...