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