UPDATE FOR EF7 :
using (var db = new BloggingContext()) { var blog = db.Blogs.First(p => p.Id == Id); db.Remove(blog); db.SaveChanges(); }
UPDATE MAY 2015 . Check updated msdn docs and examples . Sample code for deleting an object with EF6:
public async Task<ActionResult> Delete(Department department) { try { db.Entry(department).State = EntityState.Deleted; await db.SaveChangesAsync(); return RedirectToAction("Index"); } catch (DbUpdateConcurrencyException) { return RedirectToAction("Delete", new { concurrencyError = true, id = department.DepartmentID }); } catch (DataException ) {
The most efficient way if you know the identifier and donβt load the object is to create a fake object and delete it
var p = new Program { Id = myId } dbContext.Program.Remove(p)
But this will not work if you really have several records with the same identifier, and you need to use the name field, and also select the correct one.
Also your last example should be
var pg = dbContext.Program.First(p => p.Id == Id && p.Name == FName); dbContext.Program.Remove(pg);
vittore
source share