CRUD Operation in Connected Scenario:

CRUD operation in connected scenario is a fairly easy task because the context automatically tracks the changes that happened in the entity during its lifetime, provided AutoDetectChangesEnabled is true, by default.

Make sure that you have created an Entity Data Model as shown in the Create Entity Data Modelsection for SchoolDB sample database.

The following example shows how you can add, update, and delete an entity in the connected scenario (within the scope of context), which in turn will execute insert, update, and delete commands on the database. Context will automatically detect the changes and update the state of an entity.

using (var context = new SchoolDBEntities())
{
var studentList = context.Students.ToList<Student>(); //Perform create operation
context.Students.Add(new Student() { StudentName = "New Student" }); //Perform Update operation
Student studentToUpdate = studentList.Where(s => s.StudentName == "student1").FirstOrDefault<Student>();
studentToUpdate.StudentName = "Edited student1"; //Perform delete operation
context.Students.Remove(studentList.ElementAt<Student>()); //Execute Inser, Update & Delete queries in the database
context.SaveChanges();
}

Note: If context.Configuration.AutoDetectChangesEnabled = false then context cannot detect changes made to existing entities so do not execute update query. You need to call context.ChangeTracker.DetectChanges() before SaveChanges() in order to detect the edited entities and mark their status as Modified.

Context detects adding and deleting entity, when the operation is performed only on DbSet. If you perform add and delete entity on a separate collection or list, then it won't detect these changes.

The following code will NOT insert or delete student. It will only update the student entity because we are adding and deleting entities from the List and not from DbSet.

using (var context = new SchoolDBEntities())
{
var studentList = context.Students.ToList<Student>(); //Add student in list
studentList.Add(new Student() { StudentName = "New Student" }); //Perform update operation
Student studentToUpdate = studentList.Where(s => s.StudentName == "Student1").FirstOrDefault<Student>();
studentToUpdate.StudentName = "Edited student1"; //Delete student from list
if (studentList.Count > )
studentList.Remove(studentList.ElementAt<Student>()); //SaveChanges will only do update operation not add and delete
context.SaveChanges();
}

Learn how to do the CRUD operation in the disconnected scenario in the next sections.

Entity Framework Tutorial Basics(21):CRUD Operation in Connected Scenario的更多相关文章

  1. Entity Framework Tutorial Basics(1):Introduction

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

  2. Entity Framework Tutorial Basics(4):Setup Entity Framework Environment

    Setup Entity Framework Environment: Entity Framework 5.0 API was distributed in two places, in NuGet ...

  3. Entity Framework Tutorial Basics(43):Download Sample Project

    Download Sample Project: Download sample project for basic Entity Framework tutorials. Sample projec ...

  4. Entity Framework Tutorial Basics(42):Colored Entity

    Colored Entity in Entity Framework 5.0 You can change the color of an entity in the designer so that ...

  5. Entity Framework Tutorial Basics(41):Multiple Diagrams

    Multiple Diagrams in Entity Framework 5.0 Visual Studio 2012 provides a facility to split the design ...

  6. Entity Framework Tutorial Basics(37):Lazy Loading

    Lazy Loading: One of the important functions of Entity Framework is lazy loading. Lazy loading means ...

  7. Entity Framework Tutorial Basics(36):Eager Loading

    Eager Loading: Eager loading is the process whereby a query for one type of entity also loads relate ...

  8. Entity Framework Tutorial Basics(34):Table-Valued Function

    Table-Valued Function in Entity Framework 5.0 Entity Framework 5.0 supports Table-valued functions o ...

  9. Entity Framework Tutorial Basics(33):Spatial Data type support in Entity Framework 5.0

    Spatial Data type support in Entity Framework 5.0 MS SQL Server 2008 introduced two spatial data typ ...

随机推荐

  1. NOI 模拟赛 #3

    打开题一看,咦,两道数数,一道猫式树题 感觉树题不可做呀,暴力走人 数数题数哪个呢?感觉置换比矩阵好一些 于是数了数第一题 100 + 0 + 15 = 115 T1 bishop 给若干个环,这些环 ...

  2. shell 实现闰年的判断

    #!/bin/shecho "please input the year"read year let "n1=$year % 4"let "n2=$y ...

  3. redis之 centos 6.7 下安装 redis-3.2.5

    前期准备:1. 操作系统需要安装 gcc 包 与  TCL 库, 通过配置本地 yum 源 ,yum -y install gcc . yum -y install tcl安装2. 下载 redis ...

  4. Python collections系列之单向队列

    单向队列(deque) 单项队列(先进先出 FIFO ) 1.创建单向队列 import queue q = queue.Queue() q.put(') q.put('evescn') 2.查看单向 ...

  5. docker 镜像自动升级脚本

    #!/bin/bash # Let's finish it like a flash ARGS=`getopt -o v:"$@"` echo $# if [ $# != 2 ]; ...

  6. unix下网络编程之I/O复用(一)

    什么是I/O复用? What we need is the capability to tell the kernel that we want to be notified if one or mo ...

  7. 在Mac系统下如何恢复SourceTree全局忽略的文件

    在家目录“~”下编辑 “.gitignore_global ” 文件即可:vim  .gitignore_global

  8. Spark Tungsten in-heap / off-heap 内存管理机制--待整理

    一:Tungsten中到底什么是Page? 1. 在Spark其实不存在Page这个类的.Page是一种数据结构(类似于Stack,List等),从OS层面上讲,Page代表了一个内存块,在Page里 ...

  9. String是基本的数据类型吗?

    String不是基本的数据类型,是final修饰的java类,java中的基本类型一共有8个,它们分别为: 1 字符类型:byte,char 2 基本整型:short,int,long 3 浮点型:f ...

  10. 侯捷STL学习(三)--分配器测试

    第七节:分配器测试 标准的分配器Allocator,#include<ext/...>都是拓展的 可以用不同的分配器测试同一容器 分配器allocate() & deallocat ...