2012-12-31 32 views
1

我有這樣如何使用Fluent NHibernate映射包含與父類型相同類型的實體的列表?

public class Person 
{ 
    public virtual int Pkey { get; set; } 
    public virtual string Name { get; set; } 

    public List<Person> Friends{ get; set; } 
} 

和其表信息的實體就是這樣

create table Person 
(
    PKey int not null IDENTITY, 
    Name varchar (20), 
    primary key (PKey) 
) 

得到朋友的名單,我保持這樣的

Create table Friends 
(
    PKey int not null IDENTITY, 
    PersonFKey int not null Foreign key references Person(PKey), 
    FriendFKey int not null Foreign key references Person(PKey) 
) 

現在另一個表當我做如下映射,我得到一些錯誤(因爲映射問題)

public class PersonMap : ClassMap<Person> 
{ 
    public PersonMap() 
    { 
     Id(x => x.Pkey); 
     Map(x => x.Name); 
     HasManyToMany(x => x.Friends).Cascade.All().Table("Friends").ParentKeyColumn("PersonFKey"); 
    } 
} 

拋出異常是,

FluentConfigurationException: "An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail." 

隨着內部異常,

InvalidProxyTypeException: The following types may not be used as proxies: 

FluentNhibernateLearning.Entities.Person: method get_Friends should be 'public/protected virtual' or 'protected internal virtual' 
FluentNhibernateLearning.Entities.Person: method set_Friends should be 'public/protected virtual' or 'protected internal virtual' 

誰能幫我指點,我缺少的是什麼?

+0

任何原因,你沒不要嘗試做例外指示,只是改爲「公共虛擬列表朋友{get;組; }'? –

回答

4

您沒有說明錯誤是什麼,並且映射與類不匹配,但我認爲問題在於您缺少ChildKeyColumn聲明。在多對多的地圖中,您必須聲明父鍵和子鍵列;父鍵是包含集合的類的主鍵,子鍵是集合中類的主鍵。

另外,您幾乎不希望級聯多對多,因爲這會導致刪除以刪除所有相關實體。也就是說,刪除一個人會刪除他們的所有朋友。

public class IntermediaryMap : ClassMap<Intermediary> 
{ 
    public IntermediaryMap() 
    { 
     Id(x => x.Pkey); 
     Map(x => x.Name); 
     HasManyToMany(x => x.SubBrokers).Table("Intermediary2SubBroker") 
      .ParentKeyColumn("IntermediaryFKey") 
      .ChildKeyColumn("SubBrokerFKey") 
      .AsSet(); 
    } 
} 
0

我認爲你需要聲明朋友,虛擬

這就是內部異常的消息告訴你,當它說:

「方法get_Friends應該是‘公共 /保護虛擬’或‘受保護的內部虛擬’」

相關問題