Lerning Entity Framework 6 ------ Working with in-memory data
Sometimes, you need to find some data in an existing context instead of the database. By befault, Entity Framework always find data in database. If you want to find data which have loaded in memory, please do it like this:
Frist of all, let's insert some data for testing:

Then, Write some codes:
class Program
{
static void Main(string[] args)
{
using (MyContext db = new MyContext())
{
var person = db.People.Find(1);
var anotherPersons = db.People.Where(p => p.Age > 0);
int count = anotherPersons.Count();
}
Console.ReadLine();
}
}
public class Person
{
public int PersonId { get; set; }
public int Age { get; set; }
[MaxLength(50)]
public string Name { get; set; }
}
public class MyContext:DbContext
{
public MyContext():base("name=Test")
{
DbInterception.Add(new MyCommandInterceptor());
}
public DbSet<Person> People { get; set; }
}
class MyCommandInterceptor : DbCommandInterceptor
{
public override void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
base.NonQueryExecuted(command, interceptionContext);
}
public override void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
{
base.ReaderExecuted(command, interceptionContext);
Console.WriteLine("----------------------");
Console.WriteLine(command.CommandText);
Console.WriteLine();
}
public override void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
base.ScalarExecuted(command, interceptionContext);
}
}
Run the codes, you will find two SQL statments are excuted:

Then, mondify the codes:
static void Main(string[] args)
{
using (MyContext db = new MyContext())
{
var person = db.People.Find(1);
var anotherPersons = db.People.Local.Where(p => p.Age > 0);
int count = anotherPersons.Count();
}
Console.ReadLine();
}
Run it again:

That's all.
Lerning Entity Framework 6 ------ Working with in-memory data的更多相关文章
- Lerning Entity Framework 6 ------ Defining Relationships
There are three types of relationships in database. They are: One-to-Many One-to-One Many-to-Many Th ...
- 精进不休 .NET 4.5 (12) - ADO.NET Entity Framework 6.0 新特性, WCF Data Services 5.6 新特性
[索引页][源码下载] 精进不休 .NET 4.5 (12) - ADO.NET Entity Framework 6.0 新特性, WCF Data Services 5.6 新特性 作者:weba ...
- Lerning Entity Framework 6 ------ Handling concurrency With SQL Server Database
The default Way to handle concurrency of Entity Framework is using optimistic concurrency. When two ...
- Lerning Entity Framework 6 ------ Inserting, Querying, Updating, and Deleting Data
Creating Entities First of all, Let's create some entities to have a test. Create a project Add foll ...
- Lerning Entity Framework 6 ------ Defining the Database Structure
There are three ways to define the database structure by Entity Framework API. They are: Attributes ...
- Lerning Entity Framework 6 ------ Introduction to TPH
Sometimes, you have created two models. They have the same parent class like this: public class Pers ...
- Entity Framework Code First (三)Data Annotations
Entity Framework Code First 利用一种被称为约定(Conventions)优于配置(Configuration)的编程模式允许你使用自己的 domain classes 来表 ...
- Lerning Entity Framework 6 ------ Complex types
Complex types are classes that map to a subset of columns of a table.They don't contains key. They a ...
- Lerning Entity Framework 6 ------ Using a commandInterceptor
Sometimes, We want to check the original sql statements. creating a commandInterceptor is a good way ...
随机推荐
- js:作用域总结1
先说几个概念: 1.js代码从上往下执行 2.变量提升: 变量提升是浏览器的一个功能,在运行js代码之前,浏览器会给js一个全局作用域叫window,window分两个模块,一个叫内存模块,一个叫运行 ...
- SpringBoot +Pom.xml工程资源文件配置
继承spring-boot-starter-parent 要成为一个spring boot项目,首先就必须在pom.xml中继承spring-boot-starter-parent,同时指定其版本 & ...
- 动画优化、客户端存储、历史记录、worker
一.requestAnimationFrame 1.requestAnimationFrame怎么用? 设置关键帧动画效果,注重关键帧执行的情况,用法与setTimeout一样 2.requestAn ...
- spfa与dijkstra(最短路)
spfa: void spfa(){ queue<int> que; while(!que.empty()){que.pop();} que.push(s); vis[s]=; while ...
- Scrollview包裹布局问题。
输入框获取焦点,键盘弹出,背景图片上移: https://blog.csdn.net/wljian1/article/details/79962802 android:scrollbarThumbVe ...
- Python中的线程详解
线程 常用的方法 import threading import time def hello(name): print('Hello %s' % name) # 阻塞 time.sleep(5) p ...
- Individual
individual 英[ˌɪndɪˈvɪdʒuəl] 美[ˌɪndəˈvɪdʒuəl] adj. 个人的; 个别的; 独特的; n. 个人; 个体; [例句]They wait for the gr ...
- Python练习-生成器、迭代器-2018.12.01
如果列表元素可以按照某种算法推算出来,可以在循环的过程中不断推算出后续的元素.这样就不必创建完整的list,从而节省大量的空间.在Python中,这种一边循环一边计算的机制,称为生成器:generat ...
- Robot Framework浏览器驱动下载
运行robot framework 有时打不开浏览器,可能用到的驱动不对,以下是各浏览器驱动下载,仅供参考!~ 各浏览器下载地址: Firefox浏览器驱动:geckodriver https: ...
- tornado框架学习及借用有道翻译api做自动翻译页面
趁着这几天有时间,就简单的学了一下tornado框架,简单做了个自动翻译的页面 仅为自己学习参考,不作其他用途 文件夹目录结构如下: . ├── server.py ├── static │ └─ ...