2011-05-14 166 views
0

我有相同的類:FluentNHibernate映射

public class Card : EntityBase 
{ 
    private IList<Accident> accidents; 
    public Card() 
    { 
     Accidents = new List<Accident>(); 
    } 

    public virtual IList<Accident> Accidents 
    { 
     get { return accidents; } 
     set { value = accidents; } 
    } 

    public virtual void AddAccident(Accident accident) 
    { 
     Accidents.Add(accident); 
     accident.Card = this; 
    } 

} 

public class Accident : EntityBase 
{ 
    public Accident(){ 
    } 
    public virtual Card Card { get; set; }  

} 

及其映射

public class AccidentMap : ClassMap<Accident> 
{ 
    public AccidentMap() 
    { 
     Table("ACCIDENT"); 
     Id(x => x.Id).Not.Nullable().Column("ID_ACCIDENT"); 
     References(x => x.Card).Column("ID_CARD").Cascade.SaveUpdate(); 
    } 
} 

當我試圖挽救意外我的NullReferenceException在Accidents.Add(事故)。爲什麼? 謝謝。

回答

1

set { value = accidents; } 應該 set { accidents = value; }

+0

哎呀,我沒有注意到。謝謝 – kipregel 2011-05-14 14:36:17

+1

@kipregel:這是否解決了問題或者它是不相關的? – Brook 2011-05-14 14:37:09

+0

部分。現在我與表「CARD」列「ID_CARD」中的外鍵衝突。怎麼了? – kipregel 2011-05-14 14:55:13