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的更多相关文章

  1. 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 ...

  2. 精进不休 .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 ...

  3. 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 ...

  4. 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 ...

  5. Lerning Entity Framework 6 ------ Defining the Database Structure

    There are three ways to define the database structure by Entity Framework API. They are: Attributes ...

  6. Lerning Entity Framework 6 ------ Introduction to TPH

    Sometimes, you have created two models. They have the same parent class like this: public class Pers ...

  7. Entity Framework Code First (三)Data Annotations

    Entity Framework Code First 利用一种被称为约定(Conventions)优于配置(Configuration)的编程模式允许你使用自己的 domain classes 来表 ...

  8. 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 ...

  9. Lerning Entity Framework 6 ------ Using a commandInterceptor

    Sometimes, We want to check the original sql statements. creating a commandInterceptor is a good way ...

随机推荐

  1. Python学习:模块初识、数据类型

    1.模块初识 在Python中,模块分为两种: (1)标准库 标准库无需安装,只要在使用的时候import就可以使用了 (2)第三方库 第三方库必须下载安装之后才能够引入使用 下面介绍两个基本的模块: ...

  2. PHP安装Commposer

    一先把php加到环境变量里面测试 看一下版本号: 二,composer得安装注意安装的时候 php必须在5.59以上版本,openssl的扩展开启,pdo的扩展开启,mbstring的扩展开启 1,下 ...

  3. Android向系统日历中添加日程事件

    转自Android向系统日历中添加日程事件 总结 在项目开发中,我们有预约提醒.定时提醒需求时,可以使用系统日历来辅助提醒: 通过向系统日历中写入事件.设置提醒方式(闹钟),实现到时间自动提醒的功能: ...

  4. phpstudy+dvwa配置

    1.Apache端口被系统进程占用(Pid一般为4) -- 修改apache端口,然后通过localhost:端口号访问 2.DVWA System error - config file not f ...

  5. 自然语言处理的CNN模型中几种常见的池化方法

    自然语言处理的CNN模型中几种常见的池化方法 本文是在[1]的基础上进行的二次归纳. 0x00 池化(pooling)的作用   首先,回顾一下NLP中基本的CNN模型的卷积和池化的大致原理[2].f ...

  6. SQL Server与MySQL在“存在则更新,不存在则插入”并发处理上的一些差异。

    “存在则更新,不存在则插入的逻辑”并发情况下的处理 在sqlserver中: 在sqlserver中,是通过可序列化隔离级别+排它锁的方式来锁定一个范围来实现的当前锁定一个不存在的记录的时候,sqls ...

  7. 记账本,C,Github,service

    package service; import java.util.Collections; import java.util.List; import dao.CategoryDAO; import ...

  8. javaMail实现收发邮件(三)

    JavaMail API中定义了一个java.mail.Transport类,它专门用于执行邮件发送任务,这个类的实例对象封装了某种邮件发送协议的底层实施细节,应用程序调用这个类中的方法就可以把Mes ...

  9. pyspider--post

    #!/usr/bin/env python# -*- encoding: utf-8 -*-# Created on 2018-08-19 14:47:28# Project: HBGGZY_SBJ ...

  10. eclipse删除了文件,找回方法

    本人通过eclipse在前段时间上传svn代码的时候,代码掉完了,导致的原因是:svn服务器上有有个一样的文件夹,只是大小写不同,但是svn会认为是一样的文件夹,导致svn[]判别不了传到哪个文件夹去 ...