Entity Framework Tutorial Basics(21):CRUD Operation in Connected Scenario
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的更多相关文章
- Entity Framework Tutorial Basics(1):Introduction
以下系列文章为Entity Framework Turial Basics系列 http://www.entityframeworktutorial.net/EntityFramework5/enti ...
- 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 ...
- Entity Framework Tutorial Basics(43):Download Sample Project
Download Sample Project: Download sample project for basic Entity Framework tutorials. Sample projec ...
- 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 ...
- Entity Framework Tutorial Basics(41):Multiple Diagrams
Multiple Diagrams in Entity Framework 5.0 Visual Studio 2012 provides a facility to split the design ...
- Entity Framework Tutorial Basics(37):Lazy Loading
Lazy Loading: One of the important functions of Entity Framework is lazy loading. Lazy loading means ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- unix的输入输出操作
unix的输入输出操作 使用的头文件 #include <unistd.h> #include <stdio.h> 函数说明 ssize_t read(int fd, void ...
- 生成Texture2D纹理图片
using UnityEngine;using System.Collections; public class ProceduralTexture : MonoBehaviour{ public i ...
- angular的$q,defer,promise
$q是Angular的一种内置服务,它可以使你异步地执行函数,并且当函数执行完成时它允许你使用函数的返回值(或异常). 官网:http://docs.angularjs.cn/api/ng/servi ...
- gulp之sass 监听文件,自动编译
gulpfile.js文件如下: var gulp = require('gulp'); var sass = require('gulp-sass'); gulp.task('default', f ...
- vlan之间Hybrid端口配置
要求:1.PC1和PC2不能互相访问,但是都能访问PC3 SW1配置:vlan batch 10 20 100 interface Ethernet0/0/1 ...
- htmlunit 自动化提交/获取网页数据,自动化测试
开源组件: https://sourceforge.net/projects/htmlunit/ demo public void post() { try { WebClient client = ...
- [转载]嵌入式linux下操作GPIO
本文转自:http://blog.csdn.net/mirkerson/article/details/8464231 在嵌入式设备中对GPIO的操作是最基本的操作.一般的做法是写一个单独驱动程序,网 ...
- Jetty + Servlet 实现文件下载
Jetty非常适合做嵌入式web开发,正如Jetty的口号"Don’t deploy your application in Jetty, deploy Jetty in your appl ...
- 机器学习:PCA(高维数据映射为低维数据 封装&调用)
一.基础理解 1) PCA 降维的基本原理 寻找另外一个坐标系,新坐标系中的坐标轴以此表示原来样本的重要程度,也就是主成分:取出前 k 个主成分,将数据映射到这 k 个坐标轴上,获得一个低维的数据集. ...
- c++ 插入排序算法
第一.算法描述 直插排序很容易理解,在我们打扑克牌的时候,每一次摸完牌,都会按数字大小或者花色,插入到合适的位置,直到摸完最后一张牌,我们手中的牌已经按大小顺序排列好了.这整个过程就是一个 ...