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 ...
随机推荐
- CATransform3D 矩阵变换之立方体旋转实现细节 (转)
原文地址 http://blog.csdn.net/ch_soft/article/details/7351896 第一部分.前几天做动画,使用到了CATransform3D ,由于没有学过计算机图形 ...
- 在ios7中使用zxing
ZXing(Github镜像地址)是一个开源的条码生成和扫描库(开源协议为Apache2.0).它不但支持众多的条码格式,而且有各种语言的实现版本,它支持的语言包括:Java, C++, C#, Ob ...
- ios 加密解密(包括base64,DES)非原创
.h文件 #import <Foundation/Foundation.h> /******字符串转base64(包括DES加密)******/ #define __BASE64( tex ...
- CCTextFieldTTF 与 5种常用CCMenuItem
//继承(class HelloWorld : public cocos2d::CCLayer, public cocos2d::CCTextFieldDelegate) CCTextFieldTTF ...
- Python3.7安装pyspider
下面是Python3.7安装pyspider的方式,能安装成功但是后期有很多问题,所以不建议,请使用3.5版本的Python进行安装!!!由于要做爬虫工作,所以学习pyspider框架,下面介绍安装步 ...
- webrtc 术语
参考网址 https://webrtcglossary.com/ ORTC stands for Object-RTC.ORTC is an initiative involving Google, ...
- Angular5学习笔记 - 服务优化(十)
一.服务合并 二.验证效果
- 蓝桥杯 基础练习 BASIC-24 龟兔赛跑预测
基础练习 龟兔赛跑预测 时间限制:1.0s 内存限制:512.0MB 问题描述 话说这个世界上有各种各样的兔子和乌龟,但是研究发现,所有的兔子和乌龟都有一个共同的特点——喜欢赛跑.于是世界上各 ...
- iperf 网络测速
1.介绍 1) # ipref -g //这个最直观 2)Iperf 是一个网络性能测试工具.Iperf可以测试最大TCP和UDP带宽性能.Iperf具有多种参数和UDP特性,可以根据需要调整. ...
- Day2-VIM(四):修改
字符替换 r 单个字符替换 R 连续替换 - 更改大小写 很简单,多试试就行了 tips:4-更改连续4个字符的大小写,很有意思 单词修改 cw 从光标处修改到单词结尾 cb 从光标处修改到单词开头 ...