我已經使用已填寫表的現有數據庫在Entity Framework中生成了數據模型。嘗試訪問實體框架中的表時出現NULL引用異常
我想從表中訪問數據並填充WPF中的數據網格,但不斷收到空引用異常。
唯一的例外是這裏生成:
pubilc List<item> GetAllItems()
{
using (var context = new DbEntities())
{
if (context.items != null)
return context.items.ToList() //exception generated here
else
return new List<item>();
}
}
你的代碼中的哪一行出現異常?你是否檢查過'context.items'不是null? –
我把一個檢查if(context.items!= null)。它返回true代碼運行,我得到「對象引用未設置爲對象的實例」 – kr13
假設你使用VS,你可以在context.items上設置一個手錶來確認它是否被填充? – IllusiveBrian