Local Data

The Local property of DBSet provides simple access to the entities that are currently being tracked by the context, and have not been marked as Deleted. Local keeps track of entities whose entity state is added, modified and unchanged. For example:

 using System.Data.Entity;

 class Program
{
static void Main(string[] args)
{
using (var ctx = new SchoolDBEntities())
{
ctx.Students.Load(); ctx.Students.Add(new Student() { StudentName = "New Student" }); var std1 = ctx.Students.Find(); // find student whose id = 1
ctx.Students.Remove(std1);// remove student whose id = 1 var std2 = ctx.Students.Find(); // find student whose id = 1
std2.StudentName = "Modified Name"; // Loop over the students in context's local.
Console.WriteLine("In Local: ");
foreach (var student in ctx.Students.Local)
{
Console.WriteLine("Found {0}: {1} with state {2}",
student.StudentID, student.StudentName,
ctx.Entry(student).State);
} // Get all students from db.
Console.WriteLine("\nIn DbSet query: ");
foreach (var student in ctx.Students)
{
Console.WriteLine("Found {0}: {1} with state {2}",
student.StudentID, student.StudentName,
ctx.Entry(student).State);
} }
}
}
Output:

In Local :
Found 0: New Student with state Added
Found 2: Modified Name with state Modified
Found 3: Student3 with state UnchangedIn DbSet query:
Found 1: New Student with state Deleted
Found 2: Modified Name with state Modified
Found 3: Student3 with state Unchanged

As you can see in the above output, local keeps track of entities whose state is Added, Modified or Unchanged where as DbSet collection contains all the entities whose state is Deleted, Modified or Unchanged.

Visit MSDN for more information on Local.

Entity Framework Tutorial Basics(35):Local Data的更多相关文章

  1. 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 ...

  2. Entity Framework Tutorial Basics(1):Introduction

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

  3. 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 ...

  4. 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 ...

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

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

  6. 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 ...

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

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

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

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

  9. 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 ...

随机推荐

  1. 寻找节点d=n的节点算法

    这里的算法是毕设过程中,自己想到的,也不知道有不有人提出过.这里就记录下发现的过程的具体的算法,以后会用到 背景描述 毕设做的是「社交网络中病毒传播的预测」,前期过程主要是模拟几个网络的数据,然后从一 ...

  2. InnoDB引擎的特点及优化方法

    1.什么是InnoDB引擎?      InnoDB引擎是MySQL数据库的另一个重要的存储引擎,正成为目前MySQL AB所发行的新版的标准,被包含在所有二进制安装包里,和其他存储引擎相比,Inno ...

  3. tensorflow 学习笔记-1

    http://www.jianshu.com/p/e112012a4b2d 参考的网站 -------------------------------------------------------- ...

  4. CodeForces - 827A:String Reconstruction (基础并查集)

    Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun ...

  5. Git学习原版手稿

    手稿诞生记        Git学习的时候难免会有遗忘然后往复学习查看的过程,所以就形成了这个学习的手稿,记录了Git使用过程中的大部分命令,今天在清理的时候偶然看到了这些记录,而且最近也在写Git的 ...

  6. docker下安装 Oracle11gR2

    这是第二次安装,在第一次安装过程部分内容参考自如下: http://blog.sina.com.cn/s/blog_d840ff330102v4j0.html docker下oracle11g安装 h ...

  7. 小组项目需求——NABCD

    关于项目微食堂的NABCD: N(need): 就我自己而言,每天在上午三四节课时就会想中午吃什么.而且大部分的课在二教,离食堂较远.可能会面临“抢不到饭”的问题.首先不知道去哪吃.吃什么,尤其在时间 ...

  8. BZOJ4923 [Lydsy1706月赛]K小值查询

    题意 维护一个长度为n的正整数序列a_1,a_2,...,a_n,支持以下两种操作: 1 k,将序列a从小到大排序,输出a_k的值. 2 k,将所有严格大于k的数a_i减去k. \(n \leq 10 ...

  9. git之reset

    有时候,一直用公司电脑开发的时候,顺顺利利.回到家,用自己笔记本,想改改代码,git pull后,发现一堆的unmerged或者其他冲突等问题. 明明只是git pull 一下,怎么会这么多问题. 这 ...

  10. Filebeat 5.x 日志收集器 安装和配置

    Filebeat 5.x版本 风来了.fox 1.下载和安装 https://www.elastic.co/downloads/beats/filebeat 这里选择 LINUX 64-BIT 即方式 ...