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. 清除eclipse,STS workspace历史记录

    记一下 打开eclipse下的/configuration/.settings目录 修改文件org.eclipse.ui.ide.prefs文件 把RECENT_WORKSPACES这项修改为你需要的 ...

  2. .NET Entity Framework基本使用方法

    生成模型 EF有两种查询方式,Linq查询 .Lambda表达式 //普通查询 Linq 方式 IQueryable<Book> list = from b in db.Set<Bo ...

  3. 《Effective Java 第三版》新条目介绍

    版权声明:本文为博主原创文章,可以随意转载,不过请加上原文链接. https://blog.csdn.net/u014717036/article/details/80588806前言 从去年的3月份 ...

  4. shell脚本运行java程序jar

    在UBuntu上部署项目的时候,我们往往通过一段shell来启动程序,甚至是通过crontab定时任务来定时的调用java程序,但是很奇怪的一个问题就是,比如我写了一个如下的shell脚本: #!/b ...

  5. 学习node.js 第2篇 介绍node.js 安装

    Node.js - 环境安装配置 如果愿意安装设置Node.js环境,需要计算机上提供以下两个软件: 一.文本编辑器 二.Node.js二进制安装包 文本编辑器 这将用来编写程序代码. 一些编辑器包括 ...

  6. 动画优化、客户端存储、历史记录、worker

    一.requestAnimationFrame 1.requestAnimationFrame怎么用? 设置关键帧动画效果,注重关键帧执行的情况,用法与setTimeout一样 2.requestAn ...

  7. [转]MAC系统下Sublime Text3 配置Python3详细教程(亲测有效)

    原文地址: https://blog.csdn.net/weixin_41768008/article/details/79859008?tdsourcetag=s_pctim_aiomsg 这段时间 ...

  8. 【python深入】获取对象类型及属性

    在python中,查看当前的对象所能够调用的所有方法? 查看类型可以通过type,也可以通过isinstance方法,查看属性可以通过dir() 下面是对type的介绍: ————>基本类型的判 ...

  9. Extension-valuepart

    data: ls_extension type bapiparex. loop at extensionin into ls_extension . read table extensionout i ...

  10. 机器学习(三)--------多变量线性回归(Linear Regression with Multiple Variables)

    机器学习(三)--------多变量线性回归(Linear Regression with Multiple Variables) 同样是预测房价问题  如果有多个特征值 那么这种情况下  假设h表示 ...