Entity Framework Tutorial Basics(35):Local Data
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);
} }
}
}
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的更多相关文章
- 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 ...
- Entity Framework Tutorial Basics(1):Introduction
以下系列文章为Entity Framework Turial Basics系列 http://www.entityframeworktutorial.net/EntityFramework5/enti ...
- 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 ...
- 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 ...
- Entity Framework Tutorial Basics(43):Download Sample Project
Download Sample Project: Download sample project for basic Entity Framework tutorials. Sample projec ...
- 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 ...
- Entity Framework Tutorial Basics(41):Multiple Diagrams
Multiple Diagrams in Entity Framework 5.0 Visual Studio 2012 provides a facility to split the design ...
- Entity Framework Tutorial Basics(37):Lazy Loading
Lazy Loading: One of the important functions of Entity Framework is lazy loading. Lazy loading means ...
- 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 ...
随机推荐
- dirent.h
#include <dirent.h> 是POSIX.1标准定义的unix类目录操作的头文件,包含了许多UNIX系统服务的函数原型,例如opendir函数.readdir函数. opend ...
- Sortable
d_(:з」∠)_ import React, {Component} from 'react'; import "./app.css"; import Sortable from ...
- poj2336
题目大意:一个船要把n个车渡过河 船最多载m辆车 把车运过去需要t的时间 回来也要t的时间 给定n辆车依次到河边的时间 求最短运送时间 还有最短跑几趟 一维dp 可以直接d运送时间 dp[i] ...
- MySQL 用户权限详细汇总(转)
1,MySQL权限体系 MySQL 的权限体系大致分为5个层级: 全局层级: 全局权限适用于一个给定服务器中的所有数据库.这些权限存储在mysql.user表中.GRANT ALL ON .和REVO ...
- 用PHP编写登陆界面
网页的编写用PHP最方便.用php做了最简单的用户登录.创建的程序. 一. MySQL的设计 MySQL设计了两个表:members和sex.两张表的创建语句分别是: create table mem ...
- linux下的时间
1.linux下时间管理机制: 在系统启动时,Linux操作系统将时间从CMOS中读到系统时间变量中,以后修改时间通过修改系统时间实现.为了保持系统时间与CMOS时间的一致性,Linux每隔11分钟会 ...
- oracle的自增长
mysql的自增长非常容易,一个 AUTO_INCREMENT 就搞定,可是oracle就不行了 下面是oracle的自增长 #创建一个表CREATE TABLE T_TEST_DEPARTMENTS ...
- L3-001. 凑零钱(dfs或者01背包)
L3-001. 凑零钱 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 韩梅梅喜欢满宇宙到处逛街.现在她逛到了一家火星店里,发现 ...
- 第九课 go的循环语句
1 for语句的三种形式 /* for 循环 */ ; a < ; a++ { fmt.Printf("a 的值为: %d\n", a) } var a, b int = 1 ...
- 版本管理 word 文档比较
1.因为公司还在用SVN, 2.而且 还在用word 写文档, 3.而且 commit log 基本不写, 所以导致,想了解word文档 改动, 很浪费时间!!!! 所以想 快速了解word 改动, ...