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. C# 如何将对象写入文件

    http://wenku.baidu.com/link?url=QwDRlO1TeoubnmtUOitXXTRa-eZ6QFKvEuyXyzLXD9c0qCRUV5TL9Fq7_HqvxrMcwsAL ...

  2. UVALive - 4270 Discrete Square Roots (扩展欧几里得)

    给出一组正整数$x,n,r$,使得$r^2\equiv x(mod\: n)$,求出所有满足该等式的$r$. 假设有另一个解$r'$满足条件,则有$r^2-r'^2=kn$ 因式分解,得$(r+r') ...

  3. freemarker实现第一个HelloWorld

    第一步:引入freemarker jar包 第二步:创建templates下的test01.ftl 第三步:在web.xml下 第四步:编写后台代码 package com.wisezone.test ...

  4. PostgreSQL学习手册 性能提升技巧

    http://www.cnblogs.com/mchina/archive/2012/08/11/2537393.html 一.使用EXPLAIN:    PostgreSQL为每个查询都生成一个查询 ...

  5. js整数千分化

    function numToQfh(num){ var num_str = num.toString(); var strTo = " "; while(num_str.lengt ...

  6. Unity3D自定义资源配置文件

    http://blog.csdn.net/candycat1992/article/details/52181814 写在前面 我竟然最近两天才知道Unity中ScriptableObject的存在… ...

  7. mac下完全卸载mysql的方法

    sudo rm /usr/local/mysqlsudo rm -rf /usr/local/mysql*sudo rm -rf /Library/StartupItems/MySQLCOMsudo ...

  8. 第八篇 web开发学习资源

    互联网时代,最好的资源都在网上,好好利用网络学起来! 偶然才发现好资源,很多是E文的,看来努力的路还很长! 1)下面是一个老外收集的PHP资源,确实要为此分享点赞. https://github.co ...

  9. webrtc 术语

    参考网址 https://webrtcglossary.com/ ORTC stands for Object-RTC.ORTC is an initiative involving Google, ...

  10. 编译使用CEF2623遇到的错误解决办法

    https://cmake.org/download/win10的同学注意了按右键以管理员模式启动cmake-gui.exe在Where is the source code:里填上你解压的CEF3路 ...