2011-09-21 173 views
2

我一直在關注Bruno的優秀article,以實現流暢的Nhibernate中的一對一映射。然而,我遇到了一個小麻煩,同時映射了一個不明確的錯誤的私有實體。這裏是我的實體,映射和錯誤:Reveal.Member問題與Fluent和Nhibernate映射一對一映射

實體

public class Student 
{ 
    public virtual String Studentid { get; set; }  
    public virtual Address Address { get; set; }    
} 
public class Address 
{ 
    private String StudentId { get; set; } 
    private Student Student { get; set; } 

    public Address(Student student) 
    { Student = student; } 
} 

映射:

public StudentMap() 
    { 
     Id(x => x.Studentid).GeneratedBy.Assigned();   
     HasOne(x => x.Address).Cascade.All(); 
    } 

public AddressMap() 
    { 
     Id(x=> Reveal.Member<Address>("StudentId")) 
        .GeneratedBy.Foreign("Student"); 

     HasOne(x=> Reveal.Member<Address,Student>("Student"))        
       .Constrained() 
       .ForeignKey(); 

    } 

現在,當我試圖運行它,它給錯誤爲:

Could not determine type for: System.Linq.Expressions.Expression 1[[System.Func 2[[CastleTest.Domain.Address, CastleTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[System.Object, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, for columns: NHibernate.Mapping.Column(Member)

爲什麼會發生此錯誤?

回答

2

變化

HasOne(x => Reveal.Member<Address,Student>("Student")) 

HasOne(Reveal.Member<Address,Student>("Student")) 

因爲FNH認爲x => Reveal.Memberx => x.Member

通話Reveal.Member<Address,Student>("Student")回報像adress => adress.Student

+0

感謝很多答案。你能告訴我兩者之間有什麼區別嗎?雖然它運作正常......理所當然需要知道爲什麼它給了錯誤。因爲我是流利的nhibernate的新手 – Joy

+0

'x => x.Student'是一個lambda表達式,它被FNH解析以獲取屬性名,但是如果屬性不公開,你不能在lambda中訪問它,顯示'構造lambda。所以'x => Reveal.Member'將被解析以獲得Propertyname'Member' – Firo