Delete Entity using DBContext in Disconnected Scenario:

We used the Entry() method of DbContext to mark EntityState as Modified in the previous chapter. In the same way, we can use the Entry() method to attach a disconnected entity to the context and mark its state to Deleted.

Student studentToDelete;
//1. Get student from DB
using (var ctx = new SchoolDBEntities())
{
studentToDelete = ctx.Students.Where(s => s.StudentName == "Student1").FirstOrDefault<Student>();
} //Create new context for disconnected scenario
using (var newContext = new SchoolDBEntities())
{
newContext.Entry(studentToDelete).State = System.Data.Entity.EntityState.Deleted; newContext.SaveChanges();
}

The code shown above results in the following delete query which deletes the row from Teacher table.

delete [dbo].[Student]
where ([StudentId] = @0)',N'@0 int',@0=1

Thus, you can delete a single entity in disconnected scenario.

最新文章

  1. 嵌入式Linux驱动学习之路(十八)LCD驱动
  2. POJ1276Cash Machine[多重背包可行性]
  3. python datetime模块参数详解
  4. highcharts 结合phantomjs纯后台生成图片系列二之php
  5. 为何要fork()两次来避免产生僵尸进程??
  6. 如何由XSD自动生成XML和实体类
  7. Handler消息机制实现更新主UI
  8. EF中的EntityState几个状态的说明
  9. 【Unity】Unity中C#与Android中Java的互相调用遇到的一些问题
  10. 微信小程序大全(下)(最新整理 建议收藏)
  11. 【笔记】Python基础四:迭代器和生成器
  12. java框架之SpringBoot(6)-Restful风格的CRUD示例
  13. PowerTCP FTP for .NET 在线e文文档
  14. 【LeetCode】4. 寻找两个有序数组的中位数
  15. 关于 wsdl2Java 自动生成客户端调取webservice接口
  16. perl 入门知识(1)
  17. [转]CNN目标检测(一):Faster RCNN详解
  18. word公式编辑器公式
  19. JS:二维数组排序和获取子级元素
  20. 【LG1393】动态逆序对

热门文章

  1. HihoCoder1070 区间最小值(简单线段树)
  2. Eclipse中Maven配置操作
  3. LeetCode LFU Cache
  4. 上传文件csv 导入功能
  5. Linux评估 CPU使用情况
  6. Windows 7 下将 Tomcat Java 程序设置为 Windows Service
  7. Oracle 数据库迁移到MySQL (kettle,navicate,sql developer等工具
  8. 调试json
  9. 装饰器api
  10. cron job error : c queue max run limit reached