I have a wired problem in my ASP.NET MVC application using NHibernate.
There is a virutal method named IsLeaderBanker(IbEmployee banker)
in my domain model. The impelementaion is like this:
public virtual bool IsLeaderBanker(IbEmployee banker)
{
return GetLeaderBankers().Any(lb => lb.Id == banker.Id);
}
It is simple and smooth. However, Nhibernate throws an Exception when initializing SessionStorage.
The entity '<>c__DisplayClass9' doesn't have an Id mapped. Use the Id method to map your identity property. For example: Id(x => x.Id).
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: FluentNHibernate.Visitors.ValidationException: The entity '<>c__DisplayClass9' doesn't have an Id mapped. Use the Id method to map your identity property. For example: Id(x => x.Id).
Source Error:
Line 144: storage = new WebSessionStorage(this);
Line 145: NHibernateInitializer.Instance().InitializeNHibernateOnce(
Line 146: () => NHibernateSession.Init(storage,
Line 147: new[] { Server.MapPath(@"~/bin/IB.Oss.Dal") },
Line 148: AutoPersistenceModelGenerator.Generate(
After tried quite a lot of guesses and tests, I found it is the LINQ Any method made it crash. Which is, if I rewrite it like this:
public virtual bool IsLeaderBanker(IbEmployee banker)
{
var result = GetLeaderBankers();
foreach (var b in result)
{
if (b.Id == banker.Id)
{
return true;
}
}
return false;
}
Everything works. This is the same logic and Reshaper suggests me to change it to LINQ. Why the Any method breaks NHibernate? I use Where and Select many times and they all work fine.
The Nhibernate Version is 3.3.2 in my application.