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的更多相关文章

  1. Entity Framework Tutorial Basics(24):Update Single Entity

    Update Existing Entity using DBContext in Disconnected Scenario: In this chapter, you will learn how ...

  2. 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 ...

  3. Entity Framework Tutorial Basics(20):Persistence in Entity Framework

    Persistence in Entity Framework There are two scenarios when persisting an entity using EntityFramew ...

  4. 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 ...

  5. 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 ...

  6. Entity Framework Tutorial Basics(1):Introduction

    以下系列文章为Entity Framework Turial Basics系列 http://www.entityframeworktutorial.net/EntityFramework5/enti ...

  7. 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 ...

  8. Entity Framework Tutorial Basics(19):Change Tracking

    Change Tracking in Entity Framework: Here, you will learn how entity framework tracks changes on ent ...

  9. Entity Framework Tutorial Basics(7):DBContext

    DBContext: As you have seen in the previous Create Entity Data Model section, EDM generates the Scho ...

随机推荐

  1. java实现sendemail

    <dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</arti ...

  2. js 自定义方法 设置可选参数的方法

    原链接 http://www.cnblogs.com/RightDear/p/3156652.html PHP有个很方便的用法是在定义函数时可以直接给参数设默认值,如: function simue ...

  3. Maven里头的pom.xml配置详解

    正常的pom配置文件如下所示: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http ...

  4. MongoDB数据库的备份和恢复

    MongoDB数据库备份方式: 1.整库备份 2.单表备份 1.整库备份 备份整个数据库: mongodump -h 127.0.0.1:27000 -d park --authenticationD ...

  5. jira python操作,自动创建问题

    jira web api地址 http://jira.**.com/plugins/servlet/restbrowser http://jira.**.com/rest/api/2/issue/cr ...

  6. UEditor富文本编辑器的使用 http://fex.baidu.com/ueditor/

    [转] http://fex.baidu.com/ueditor/ UEditor 介绍 UEditor 是由百度「FEX前端研发团队」开发的所见即所得富文本web编辑器,具有轻量,可定制,注重用户体 ...

  7. -3dB的理解

    -3dB到底是什么?集成运放-3dB带宽又是什么? 以无源高通电路为例,介绍-3dB的意义. 输出与输入只比: Au=Uo/Ui=R/(R+1/j*2*PI*f*C)=1/(1+1/j*2*PI*f* ...

  8. 蓝桥杯 历届试题 PREV-1 核桃的数量

    历届试题 核桃的数量   时间限制:1.0s   内存限制:256.0MB 问题描述 小张是软件项目经理,他带领3个开发组.工期紧,今天都在加班呢.为鼓舞士气,小张打算给每个组发一袋核桃(据传言能补脑 ...

  9. Java-API:java.util.list

    ylbtech-Java-API:java.util.list 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部 1. https://docs.oracle.co ...

  10. .netcore 在linux 上运行 Helllo World 例子

    要想在linux上运行netcore ,需要在linux 安装netcore SKD 下面分两步完成, 首先我的操作系统是 Ubuntu 14.04 ,不同版本可以参考 官网给出的连接 https:/ ...