I am trying to load object Supplier from my database. Supplier has a foreign key Adress,so I use Include method to load Supplier,but I got an NULL REFERENCE EXCEPTION. This is my code in Controller:
public ActionResult LoadSupplier(Supplier supplier)
{
if (supplier.SupplierID==0)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Supplier sup= db.Supplier
.Include(d => d.Adress)
.FirstOrDefault(x=>x.SupplierID==supplier.SupplierID);
if (sup== null)
{
return HttpNotFound();
}
return View(sup);
}
Also i try Explicit loading,but also got null reference:
var sup= db.Supplier.Single(x=>x.SupplierID==supplier.SupplierID);
db.Entry(sup).Reference(d => d.Adress).Load();
I appreciate any advice or help!