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. HDU2065"红色病毒"问题【指数型母函数】

    Problem Description 医学界发现的新病毒因其蔓延速度和Internet上传播的"红色病毒"不相上下,被称为"红色病毒",经研究发现,该病毒及其 ...

  2. 重温CLR(一)CLR基础

    如果一个C#developer,对CLR没有了解,那就只能是入门级别.未来.NET CORE是趋势,但是.NET CORE 也是基于CoreCLR的,而CLR和CoreCLR其实差别不大,从runti ...

  3. karma

    一个简单的工具,允许你在多个浏览器中执行JavaScript代码. Karma的主要目的是使您的测试驱动开发变得简单.快速和有趣. 我什么时候该用Karma? 您希望在真正的浏览器中测试代码. 您希望 ...

  4. debian7.2+nginx+mysql

    1.安装mysql sudo aptitude install mysql-server netstat -atln 查看3306端口 数据库目录: /var/lib/mysql/ 配置文件/usr/ ...

  5. UEditor富文本编辑器的图片上传 http://fex.baidu.com/ueditor/#server-deploy

    http://fex.baidu.com/ueditor/#server-deploy http://fex.baidu.com/ueditor/#server-path 首先 editor配置文件中 ...

  6. 蓝桥杯 算法训练 ALGO-115 和为T

    算法训练 和为T   时间限制:1.0s   内存限制:256.0MB 问题描述 从一个大小为n的整数集中选取一些元素,使得它们的和等于给定的值T.每个元素限选一次,不能一个都不选. 输入格式 第一行 ...

  7. [转载]Linux内核list_head学习(二)

    前一篇文章讨论了list_head 结构的基本结构和实现原理,本文主要介绍一下实例代码. 自己如果想在应用程序中使用list_head 的相应操作(当然应该没人使用了,C++ STL提供了list 用 ...

  8. Python内置:items()方法

    文章转载于:https://www.cnblogs.com/wushuaishuai/p/7738118.html(博主:IT技术随笔) #Python3中已取消iteritems()方法 描述 Py ...

  9. java继承如何理解呢??

    总结:我把他弄的无语了.他是诺基亚公司的软件开发师,大学毕业就可以进那么好的公司.实力 package com.bc; //普通类 class yt { public void price() { S ...

  10. MacOS配置Erlang开发环境

    Mac下安装Erlang brew 的安装: $ curl -LsSf http://github.com/mxcl/homebrew/tarball/master | sudo tar xvz -C ...