0

我在我的數據庫中有一個名爲Impacts的表,它有兩個foring鍵,從組件表調用 cmpt_name和cmpt_reference。如何將數據添加到實體框架中的ObservableCollection?

我想將一些數據添加到我使用影響表創建的ObservableCollection中。 但我不能添加cmpt_name和cmpt_reference它?

public ObservableCollection<Impact> ModelListe { get; set; } 

private Impact model; 

public Project_Questions_Window() 
{ 
    InitializeComponent(); 

    ModelListe = new ObservableCollection<Impact>(); 
    DataContext = this; 
} 

public void addData() 
{ 
    model = new Impact(); 
    **model.Component.cmpt_name = comboBoxComponents.Text;** 
    model.impt_name = textBoxQuestion.Text; 
    **model.Component.cmpt_reference = comboBoxComponents.SelectedValuePath;** 
    ModelListe.Add(model); 
} 

我得到突出顯示的行錯誤,它說:

對象引用不設置到對象的實例。

任何人都可以告訴我如何解決它嗎? 即時通訊使用實體模型數據庫。

+0

你能告訴我你的'影響class'? – 2013-03-26 07:34:25

回答

1

當你新增了一個Impact實例時,我很肯定它不包含Component呢。所以model.Component這裏是空對象。

我不知道您的業務邏輯,但我認爲補救措施是從數據庫中提取Component,並使用這些預先存在的組件填充組合框。現在,當你創建一個新的Impact你不設置其組件的名字,但你設置其屬性Compenent

model.Component = comboBoxComponents.SelectedValue; 
相關問題