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. python 字符串大小写相关函数

    改写:(都不会改变原字符串) s = 'hEllo wOrld' s Out[3]: 'hEllo wOrld' s.upper()#全部大写 Out[4]: 'HELLO WORLD' s Out[ ...

  2. uva10943(隔板法)

    很裸的隔板法. 引用一下维基上对隔板法的解释: 现在有10个球,要放进3个盒子里 ●●●●●●●●●● 隔2个板子,把10个球被隔开成3个部份 ●|●|●●●●●●●●.●|●●|●●●●●●●.●| ...

  3. javascript网页复制功能-复制到粘贴板-兼容多数浏览器(不使用flash)

    使用方法:clipBordCopy("hello Copy");//执行后复制hello Copy到粘贴板 通过 var result = clipBordCopy("h ...

  4. python 获取本机ip地址的方法(Unix 平台)

    #!/usr/bin/python import socket import fcntl import struct def get_ip_address(ifname): s = socket.so ...

  5. 康托展开与逆康托展开模板(O(n^2)/O(nlogn))

    O(n2)方法: namespace Cantor { ; int fac[N]; void init() { fac[]=; ; i<N; ++i)fac[i]=fac[i-]*i; } in ...

  6. HihoCoder1070 区间最小值(简单线段树)

    个测试点(输入文件)有且仅有一组测试数据. 每组测试数据的第1行为一个整数N,意义如前文所述. 每组测试数据的第2行为N个整数,分别描述每种商品的重量,其中第i个整数表示标号为i的商品的重量weigh ...

  7. Maven实现直接部署Web项目到Tomcat7

    如题目,自动部署到Web服务器,直接上过程. 1.Tomcat7的用户及权限配置:在conf目录下,找到tomcat-users.xml,添加manager权限的用户. <role rolena ...

  8. HIVE-利用ow_number() OVER(PARTITION BY)函数介绍求TOP-K

    http://blog.csdn.net/631799/article/details/7419797 第一句话: select row_number() over (partition by mon ...

  9. 在MySQL中设置事务隔离级别有2种方法:

    在MySQL中设置事务隔离级别有2种方法: 1 在my.cnf中设置,在mysqld选项中如下设置 [mysqld] transaction-isolation = READ-COMMITTED 2 ...

  10. Poj 2395 Out of Hay( 最小生成树 )

    题意:求最小生成树中最大的一条边. 分析:求最小生成树,可用Prim和Kruskal算法.一般稀疏图用Kruskal比较适合,稠密图用Prim.由于Kruskal的思想是把非连通的N个顶点用最小的代价 ...