LINQ任何方法都会破坏NHibernate的初始化

时间:2013-06-19 08:03:14

标签: linq nhibernate

我在使用NHibernate的ASP.NET MVC应用程序中遇到了一个有线问题。

我的域模型中有一个名为IsLeaderBanker(IbEmployee banker)的虚拟方法。必要条件是这样的:

public virtual bool IsLeaderBanker(IbEmployee banker)
{
    return GetLeaderBankers().Any(lb => lb.Id == banker.Id);
}

简单流畅。但是,Nhibernate在初始化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(

经过大量的猜测和测试后,我发现它是 LINQ Any 方法让它崩溃了。也就是说,如果我像这样重写它:

public virtual bool IsLeaderBanker(IbEmployee banker)
{
    var result = GetLeaderBankers();
    foreach (var b in result)
    {
        if (b.Id == banker.Id)
        {
            return true;
        }
    }
    return false;
}

一切正常。这是相同的逻辑,Reshaper建议我将其更改为LINQ。为什么Any方法会破坏NHibernate?我多次使用Where和Select,它们都能很好地工作。

Nhibernate版本在我的应用程序中是3.3.2。

1 个答案:

答案 0 :(得分:0)

试试这个:

public virtual bool IsLeaderBanker(IbEmployee banker)
{
    var id = banker.Id;
    return GetLeaderBankers().Any(lb => lb.Id == id);
}