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 ...
随机推荐
- 【SQL查询】查询的值为空时,给出默认值_NVL函数
格式为: NVL( string1, replace_with) 功能:如果string1为NULL,则NVL函数返回replace_with的值,否则返回string1的值. 引申一下,此NVL的作 ...
- Memcache mutex设计模式
Memcache mutex设计模式 转自:https://timyang.net/programming/memcache-mutex/ 场景 Mutex主要用于有大量并发访问并存在cache过期的 ...
- 「新手向」koa2从起步到填坑
前传 出于兴趣最近开始研究koa2,由于之前有过一些express经验,以为koa还是很好上手的,但是用起来发现还是有些地方容易懵逼,因此整理此文,希望能够帮助到一些新人. 如果你不懂javascri ...
- 微信小程序编写物流信息进度样式
做电商类型的小程序一定会碰到编写物流信息的时候,一般页面如下图 难点在于只有一条信息时候的页面样式 以及多条信息最后一条信息的页面样式 之前没做过这一块的东西,所以刚碰到的时候想了老半天orz.后来上 ...
- ugui在运行时改变RectTransform的大小
http://blog.csdn.net/BeiFuDeNvWang/article/details/50838266 在代码中动态改变RectTransform大小的方法如下所示: 1:直接对siz ...
- CodeSmith 基本语法(二)
CodeSmith之四 - 典型实例(四) CodeSmith API文档 (三) CodeSmith 基本语法(二) CodeSmith 图形界面基本操作(一) CodeSmith的C#语法与Asp ...
- source In sight 中修改自动补全快捷键方式
点击 “options”中的“key Assi...”,找到如下 点击“Assign New Key...”之后按键盘上的指定按键就能重新设定.
- webrtc 术语
参考网址 https://webrtcglossary.com/ ORTC stands for Object-RTC.ORTC is an initiative involving Google, ...
- Numpy:dot()函数
转于:https://www.cnblogs.com/luhuan/p/7925790.html博主:忧郁的白衬衫 一.dot()的使用 1)格式:np.dot(array1, array2) == ...
- vBulletin 5.x 版本通杀远程代码执行漏洞复现
漏洞介绍 vBulletin中存在一个文件包含问题,可使恶意访问者包含来自 vBulletin 服务器的文件并且执行任意 PHP 代码.未经验证的恶意访问者可通过向index.php发出包含 rout ...