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. git教程2-删除修改和文件

    文件处于三种状态: 1.位于工作区,未修改状态: 2.位于工作区,已经修改状态: 3.位于暂存区,已经暂存但未commit. 4.已经commit. 一.文件删除修改: 1.已经修改,但未add: g ...

  2. 如何显示PHP运行错误

    在运行文件的最前面加两行代码: error_reporting(E_ALL); ini_set('display_errors', '1'); 这样调试起来就方便多了

  3. 使用 MLCC 替代电解电容需要注意几点 (2018-07-23)

    使用 MLCC 替代电解电容需要注意几点 容量,MLCC 在高压时容量会降到标称的 30~50% 以下 1. MLCC 的 ESR 很低,比较适合高频 DCDC 输出. MLCC 会有压电效应,可能会 ...

  4. snmpwalk用法

    snmpwalk语法:snmpwalk 交换机或路由器IP地址 -c SNMP读密码 -v 1或2(代表SNMP版本) OID(对象标示符) 用法举例:1.snmpwalk -c public -v ...

  5. CentOS7 yum安装mysql5.5/5.6并初始化

    https://blog.csdn.net/petrel2015/article/details/78822466 下载MySQL yum仓库文件 首先根据官网给出的建议,下载MySQL的仓库文件 h ...

  6. 第15届浙江省赛 D Sequence Swapping(dp)

    Sequence Swapping Time Limit: 1 Second      Memory Limit: 65536 KB BaoBao has just found a strange s ...

  7. TCG卡牌游戏研究:《炉石战记:魔兽英雄传》所做的改变

    转自:http://www.gameres.com/665306.html TCG演进史 说到卡牌游戏,大家会联想到什么呢? 是历史悠久的扑克牌.风靡全球的<MTG 魔法风云会>与< ...

  8. 利用DNS进行传输数据

    曾经有这样一道题目,困了我数个小时,最后我尝试利用此方法时我知道真相的时候,眼泪已掉下来. 遇到的是一个流量分析题,分析DNS数据,拿到flag 流量如图所示: 进入linux,提取: [root@s ...

  9. Drools学习笔记3—Conditions / LHS—字段约束连接&字段约束操作符

    字段约束连接 用于字段约束 对象内部多个约束连接,采用“&&”(and).“||”(or)和“,”(and) 执行顺序:“&&”(and).“||”(or)和“,” 字 ...

  10. python 2.7.5升级到3.4.x

    wget https://www.python.org/ftp/python/3.4.3/Python-3.4.3.tgz .tgz cd Python-/ Python ./configure ma ...