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:

  1. using System.Data.Entity;
  2.  
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. using (var ctx = new SchoolDBEntities())
  8. {
  9. ctx.Students.Load();
  10.  
  11. ctx.Students.Add(new Student() { StudentName = "New Student" });
  12.  
  13. var std1 = ctx.Students.Find(); // find student whose id = 1
  14. ctx.Students.Remove(std1);// remove student whose id = 1
  15.  
  16. var std2 = ctx.Students.Find(); // find student whose id = 1
  17. std2.StudentName = "Modified Name";
  18.  
  19. // Loop over the students in context's local.
  20. Console.WriteLine("In Local: ");
  21. foreach (var student in ctx.Students.Local)
  22. {
  23. Console.WriteLine("Found {0}: {1} with state {2}",
  24. student.StudentID, student.StudentName,
  25. ctx.Entry(student).State);
  26. }
  27.  
  28. // Get all students from db.
  29. Console.WriteLine("\nIn DbSet query: ");
  30. foreach (var student in ctx.Students)
  31. {
  32. Console.WriteLine("Found {0}: {1} with state {2}",
  33. student.StudentID, student.StudentName,
  34. ctx.Entry(student).State);
  35. }
  36.  
  37. }
  38. }
  39. }
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. Django基于form组件实现注册校验

    一 基本流程 1 创建form组件对应的类,比如LoginForm 2 前端的三种渲染方式: 渲染方式三种: 1 <form action="" novalidate met ...

  2. 烂泥Linux学习笔记

    把最近学习过程中所写的文章整理了下:注意:本帖会持续性更新!!! 虚拟化篇:<烂泥:虚拟化KVM安装与配置><烂泥:KVM安装centos6.5系统><烂泥:KVM中安装 ...

  3. Chrome MarkDown Preview Plus

    /************************************************************************** * Chrome MarkDown Previe ...

  4. loj #6216. 雪花挂饰

    (今天碰到的题怎么这么小清新 $n$ 个不相同的点,$q$ 组询问,每次给定 $l,r$,问在 $n$ 个点中,选出 $x$ 个点 $(x \in [l,r])$,用边连起来,能构成多少种不同的树 $ ...

  5. loj#6566. 月之都的密码

    搜交互题搜到的... 竟然还有这么水的交互题,赶紧过了再说 交互库里有一个 $[1,n]$ 到 $[1,n]$ 的双射 你可以调用 $encode(k,a[])$ 询问左边的一个大小为 $k$ 的集合 ...

  6. UVA - 242 Stamps and Envelope Size (完全背包+bitset)

    题意:给你一些邮票面值的集合,让你选择其中一个集合,使得“能用不超过n枚集合中的邮票凑成的面值集合S中从1开始的最大连续面值”(即mex(S)-1)最大.如果有多解,输出集合大小最小的一个:如果仍有多 ...

  7. BZOJ- 2733: 永无乡 (并查集&线段树合并)

    题意:给定N个节点,K次操作,操作有两种,1是合并两个集合,2是求某个集合的第K大(从小到大排序). 思路:合并只要启发式即可.此题可以用线段树,保存1到N的排序的出现次数和. 复杂度O(NlogN) ...

  8. asp.net core mcroservices 架构之 分布式日志(二)之自定义日志开发

    netcore日志原理 netcore的日志是作为一个扩展库存在的,每个组件都有它的入口,那么作为研究这个组件的入口是最好的,首先看两种方式: 这个是源码例子提供的. var loggingConfi ...

  9. UOJ348. 【WC2018】州区划分

    UOJ348. [WC2018]州区划分 http://uoj.ac/problem/348 分析: 设\(g(S)=(\sum\limits_{x\in S}w_x)^p[合法]\) \(f(S)\ ...

  10. Python函数-bool()

    bool([x]) 作用: 将x转换为Boolean类型,如果x缺省,返回False,bool也为int的子类: 参数x: 任意对象或缺省:大家注意到:这里使用了[x],说明x参数是可有可无的,如果不 ...