2013-06-19 25 views
1

我在我的ASP.NET MVC應用程序使用NHibernate中有一個有線問題。LINQ任何方法打破NHibernate初始化

在我的域模型中有一個名爲IsLeaderBanker(IbEmployee banker)的虛擬方法。推動力是這樣的:

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

它簡單而流暢。但是,初始化SessionStorage時,Nhibernate會引發異常。

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任何方法使其崩潰。也就是說,如果我重寫它是這樣的:

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。

+0

最好的辦法是在報告NHibernate的[問題跟蹤]一個bug(https://nhibernate.jira.com/secure/Dashboard.jspa)。 –

回答

0

試試這個:做

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