2012-11-06 197 views
0

我使用實體框架代碼首先,我有一個實體ArticoloZefiroContext : DbContext其中包含如下聲明:實體框架代碼優先的NullReferenceException

public DbSet<Articolo> Articoli { get; set; } 

當我運行下面的代碼:

var a = new ZefiroContext().Articoli; 
List<Articolo> r; 
if (a != null) 
    r = a.ToList(); 
else 
    r = new List<Articolo>(); 

我收到NullReferenceException在線r = a.ToList();。我在我的數據庫中沒有Articolo實體,所以我希望我的變量r指向一個空的List<Articolo>

+0

對不起,但爲什麼你沒有一個實體'Articolo'在你的分貝?對我來說這是錯誤。只要創建表,它會起作用! –

+0

你可以顯示異常的堆棧跟蹤嗎? – Slauma

+0

你的期望看起來非常合理。我剛剛檢查過我的設置,我可以運行相應的代碼,它確實返回一個空列表。 –

回答

0

你只需要。

List<Articolo> r = new ZefiroContext().Articoli.ToList(); 
+0

我嘗試了List r = new ZefiroContext()。Articoli.ToList();但我得到了同樣的結果。我已經有一個實體Articolo的數據庫表,它是空的 – ollo