Delete a single record from Entity Framework?
我在实体框架中有一个名为
如何使用实体框架从表中删除单个记录?
不需要先查询对象,您可以通过其ID将其附加到上下文。这样地:
1 2 3 4 | var employer = new Employ { Id = 1 }; ctx.Employ.Attach(employer); ctx.Employ.Remove(employer); ctx.SaveChanges(); |
或者,可以将附加条目的状态设置为"已删除":
1 2 3 | var employer = new Employ { Id = 1 }; ctx.Entry(employer).State = EntityState.Deleted; ctx.SaveChanges(); |
您可以使用
1 2 3 4 5 6 | var itemToRemove = Context.Employ.SingleOrDefault(x => x.id == 1); //returns a single item. if (itemToRemove != null) { Context.Employ.Remove(itemToRemove); Context.SaveChanges(); } |
1 2 3 4 5 6 7 8 | var stud = (from s1 in entities.Students where s1.ID== student.ID select s1).SingleOrDefault(); //Delete it from memory entities.DeleteObject(stud); //Save to database entities.SaveChanges(); |
1 2 3 4 | Employer employer = context.Employers.First(x => x.EmployerId == 1); context.Customers.DeleteObject(employer); context.SaveChanges(); |
我正在使用带有LINQ的实体框架。以下代码对我有帮助;
1-对于多个记录
1 2 3 4 5 6 | using (var dbContext = new Chat_ServerEntities()) { var allRec= dbContext.myEntities; dbContext.myEntities.RemoveRange(allRec); dbContext.SaveChanges(); } |
2-单记录
1 2 3 4 5 6 | using (var dbContext = new Chat_ServerEntities()) { var singleRec = dbContext.ChatUserConnections.FirstOrDefault( x => x.ID ==1);// object your want to delete dbContext.ChatUserConnections.Remove(singleRec); dbContext.SaveChanges(); } |
使用实体框架6,您可以使用
1 2 3 4 5 6 | using (var context = new EmployDbContext()) { Employ emp = context.Employ.Where(x => x.Id == id).Single<Employ>(); context.Employ.Remove(emp); context.SaveChanges(); } |
你可以这样做
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | public ActionResult Delete(int? id) { using (var db = new RegistrationEntities()) { Models.RegisterTable Obj = new Models.RegisterTable(); Registration.DAL.RegisterDbTable personalDetail = db.RegisterDbTable.Find(id); if (personalDetail == null) { return HttpNotFound(); } else { Obj.UserID = personalDetail.UserID; Obj.FirstName = personalDetail.FName; Obj.LastName = personalDetail.LName; Obj.City = personalDetail.City; } return View(Obj); } } [HttpPost, ActionName("Delete")] public ActionResult DeleteConfirmed(int? id) { using (var db = new RegistrationEntities()) { Registration.DAL.RegisterDbTable personalDetail = db.RegisterDbTable.Find(id); db.RegisterDbTable.Remove(personalDetail); db.SaveChanges(); return RedirectToAction("where u want it to redirect"); } } |
模型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | public class RegisterTable { public int UserID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Password { get; set; } public string City { get; set; } } |
你称之为它的视图
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <table class="table"> <tr> <th> FirstName </th> <th> LastName </th> <th> City </th> <th></th> </tr> @foreach (var item in Model) { <tr> <td> @item.FirstName </td> <td> @item.LastName </td> <td> @item.City</td> <td> Edit | Details | Delete </td> </tr> } </table> |
我希望这会让你容易理解
更一般的接近
1 2 3 4 5 6 7 8 9 10 11 | public virtual void Delete<T>(int id) where T : BaseEntity, new() { T instance = Activator.CreateInstance<T>(); instance.Id = id; if (dbContext.Entry<T>(entity).State == EntityState.Detached) { dbContext.Set<T>().Attach(entity); } dbContext.Set<T>().Remove(entity); } |
您可以在网格的Click或CellDoubleClick事件中执行类似的操作(如果使用的话)
1 2 3 4 5 | if(dgEmp.CurrentRow.Index != -1) { employ.Id = (Int32)dgEmp.CurrentRow.Cells["Id"].Value; //Some other stuff here } |
然后在"删除"按钮中执行如下操作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | using(Context context = new Context()) { var entry = context.Entry(employ); if(entry.State == EntityState.Detached) { //Attached it since the record is already being tracked context.Employee.Attach(employ); } //Use Remove method to remove it virtually from the memory context.Employee.Remove(employ); //Finally, execute SaveChanges method to finalized the delete command //to the actual table context.SaveChanges(); //Some stuff here } |
或者,可以使用LINQ查询,而不是使用LINQ to Entities查询:
1 2 3 | var query = (from emp in db.Employee where emp.Id == employ.Id select emp).Single(); |
ID用作筛选参数,该参数已从DataGridView的CellDoubleClick事件传递。
使用EntityFramework.Plus可以是一个选项:
1 | dbContext.Employ.Where(e => e.Id == 1).Delete(); |
这里有更多的例子
对于一般DAO,我的工作finnaly如下:
1 2 3 4 5 | public void Detele(T entity) { db.Entry(entity).State = EntityState.Deleted; db.SaveChanges(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [HttpPost] public JsonResult DeleteCotnact(int id) { using (MycasedbEntities dbde = new MycasedbEntities()) { Contact rowcontact = (from c in dbde.Contact where c.Id == id select c).FirstOrDefault(); dbde.Contact.Remove(rowcontact); dbde.SaveChanges(); return Json(id); } } |
你觉得这个怎么样,简单与否,你也可以试试这个:
1 2 3 | var productrow = cnn.Product.Find(id); cnn.Product.Remove(productrow); cnn.SaveChanges(); |