Entity Framework Tutorial Basics(25):Delete Single Entity
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.
Entity Framework Tutorial Basics(25):Delete Single Entity的更多相关文章
- Entity Framework Tutorial Basics(24):Update Single Entity
Update Existing Entity using DBContext in Disconnected Scenario: In this chapter, you will learn how ...
- Entity Framework Tutorial Basics(23):Add Single Entity
Add New Entity using DBContext in Disconnected Scenario: In this chapter you will learn how to add n ...
- Entity Framework Tutorial Basics(20):Persistence in Entity Framework
Persistence in Entity Framework There are two scenarios when persisting an entity using EntityFramew ...
- Entity Framework Tutorial Basics(8):Types of Entity in Entity Framework
Types of Entity in Entity Framework: We created EDM for existing database in the previous section. A ...
- Entity Framework Tutorial Basics(2):What is Entity Framework?
What is Entity Framework? Writing and managing ADO.Net code for data access is a tedious and monoton ...
- Entity Framework Tutorial Basics(1):Introduction
以下系列文章为Entity Framework Turial Basics系列 http://www.entityframeworktutorial.net/EntityFramework5/enti ...
- Entity Framework Tutorial Basics(31):Migration from EF 4.X
Migration from Entity Framework 4.1/4.3 to Entity Framework 5.0/6.0 To migrate your existing Entity ...
- Entity Framework Tutorial Basics(19):Change Tracking
Change Tracking in Entity Framework: Here, you will learn how entity framework tracks changes on ent ...
- Entity Framework Tutorial Basics(7):DBContext
DBContext: As you have seen in the previous Create Entity Data Model section, EDM generates the Scho ...
随机推荐
- mysql中事务隔离级别可重复读说明
mysql中InnoDB引擎默认为可重复读的(REPEATABLE READ).修改隔离级别的方法,你可以在my.inf文件的[mysqld]中配置: transaction-isolation = ...
- BZOJ - 2244 拦截导弹 (dp,CDQ分治+树状数组优化)
题目链接 dp进阶之CDQ分治优化dp. 前置技能:dp基本功底,CDQ分治,树状数组. 问题等价于求二维最长上升子序列,是一个三维偏序问题(时间也算一维). 设$dp[i]=(l,x)$为以第i枚导 ...
- PostgreSQL学习手册 性能提升技巧
http://www.cnblogs.com/mchina/archive/2012/08/11/2537393.html 一.使用EXPLAIN: PostgreSQL为每个查询都生成一个查询 ...
- js相关命令
echo "<script>alert('您尚未关注,关注成功后方可下单!');location.href='http://www.baidu.com';</script& ...
- STL容器共性机制和使用场景
一.STL容器共性机制 STL容器所提供的都是值(value)寓意,而非引用(reference)寓意,也就是说当我们给容器中插入元素的时候,容器内部实施了拷贝动作,将我们要插入的元素再另行拷贝一份放 ...
- 修改分区后的 Grub rescue
声明:这里用到的知识不是原创,综合了几篇教程的成果.找的时候比较混乱,所以来源已经不确定.希望原作者见谅. 系统是Windows 8.1 和 Ubuntu 14.04, Windows是先装的, gr ...
- keepalived之 ipvsadm-1.26-4(lvs)+ keepalived-1.2.24 安装
一.安装 LVS 前提:已经提前配置好本地 Yum 源 配置过程可参考> http://blog.csdn.net/zhang123456456/article/details/56690945 ...
- VirtualBox-Debian7.2-share
1.在VirtualBox(用的版本是4.3.6)中,选择虚拟机->设置-> 共享文件夹, 2.添加一个主机上的文件夹readhat用来作为共享文件夹,选中固定分配和自动挂载, 结果:进入 ...
- 修改crushmap实验
标签(空格分隔): ceph,ceph实验,crushmap CRUSH的全称是Controlled Replication Under Scalable Hashing,是ceph数据存储的分布式选 ...
- PowerDesigner中批量替换name和code的脚本
无论是cdm还是pdm都可以批量替换.处理.可在Tool-Execute commands-Edit/Run script中编辑运行脚本: 下面的脚本是批量将CDM中实体的用Code替换掉Name O ...