Using Entity Framework on .Net 4
Here is my code:
using (frfcourEntities frf = new frfcourEntities())
{
EntityKey routehdrId = new EntityKey("frfcourEntities.Routehdrs", "Refno", "xxx");
try{
var routehdr = frf.GetObjectByKey(routehdrId);
frf.DeleteObject(routehdr);
frf.SaveChanges();
}
catch(System.Data.ObjectNotFoundException)
{}
}
The line:
catch(System.Data.ObjectNotFoundException)
will not compile. It reports
The type caught or thrown must be derived from System.Exeception.
But ObjectNotFoundException is what the EF GetObjectByKey method throws.
Add an assembly reference to System.Data. Without it “using System.Data” works but it doesn’t resolve DataException.