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 ...
随机推荐
- angularJs 指令的封装
首先 指令的应用场景: 1:使你的html更具语义化,不需要深入研究代码就可以知道页面的大概逻辑. 2:抽象出一个自定义组件,在其他的地方进行重用. 一:directive的定义及其使用方法: 下面是 ...
- PHP获取汉字拼音首字母 截取中文字符串
http://blog.csdn.net/everything1209/article/details/39005785 substr是按字符分割,而mb_strcut是按字节来分割,但是都不会产生半 ...
- Azure VNet介绍
Azure VNet的介绍 VNet是Azure云中逻辑隔离的虚拟网络.它包含两个含义: Azure的用户可以在VNet中创建自己的各种资源,感觉想自己的数据中心中一样; 在一个VNet中创建的资源和 ...
- JSF拦截ajax请求并传递参数方法
我们可以利用f:ajax做一些简单的ajax操作,但是遇到复杂的逻辑,它不能简单的去实现,jsf提供了一种方法,可以调用它内部的js方法去实现复杂的逻辑. 首先要在页面引入jsf的js文件: < ...
- 蓝桥杯 基础练习 BASIC-22 FJ的字符串
基础练习 FJ的字符串 时间限制:1.0s 内存限制:512.0MB 问题描述 FJ在沙盘上写了这样一些字符串: A1 = “A” A2 = “ABA” A3 = “ABACABA” A4 = ...
- mysql 替换语句
将cdb_pms表subject字段中的Welcom to替换成 欢迎光临 UPDATE `cdb_pms` SET `subject` = REPLACE(`subject`, 'Welcome t ...
- get方法传递中文数据的时候如何进行转码
首先,如果是在js端的代码,用window.href进行请求时,需要进行转码 前台jsp中: var param = document.getElementById('param').value;pa ...
- 高效C#编码优化
1.foreach VS for 语句 Foreach 要比for具有更好的执行效率Foreach的平均花费时间只有for的30%.通过测试结果在for和foreach都可以使用的情况下,我们推荐使用 ...
- app中使用微信分享注意事项
1. 在微信公众平台开通一个微信公众号,https://mp.weixin.qq.com 2. 将自己制作好的已签名的app安装到手机上 3. 下载微信开放平台获取应用签名的apk--- gen ...
- Python unittest excel数据驱动 写入
之前写过一篇关于获取excel数据进行迭代的方法,今天补充上写入的方法.由于我用的是Python3,不兼容xlutils,所以无法使用copy excel的方式来写入.这里使用xlwt3创建excel ...