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 ...
随机推荐
- Shell脚本1-20例
1.每天生成一个文件 描述:请按照这样的日期格式(xxxx-xx-xx)每日生成一个文件,例如今天生成的文件为)2017-07-05.log, 并且把磁盘的使用情况写到到这个文件中,(不用考虑cron ...
- leetcode3
public class Solution { public int LengthOfLongestSubstring(string s) { var dic = new Dictionary< ...
- hitTest,UIWindow sendEvent ,touchbegan, 响应链
https://developer.apple.com/documentation/uikit/touches_presses_and_gestures/using_responders_and_th ...
- 2017-11-04 Sa Oct 消参
2017-11-04 Sa $ P(-3, 0) $ 在圆C $ (x-3)^2 + y^2 = 8^2 $ 内,动圆M与圆相切且过P点,求M点轨迹. 设切点 $ A(a, b) $,圆心 \(M(x ...
- Install weblogic in silent mode
使用静默(silent)模式来安装weblogic,在需要将安装脚本化,或无法使用图形界面的时候非常有用. 下面按照自己在实际工作中碰到的例子,来慢慢总结不同版本和平台weblogic的静默安装方法. ...
- DOCKER解析(转)
Docker基本概念详解 本文只是对Docker的概念做了较为详细的介绍,并不涉及一些像Docker环境的安装以及Docker的一些常见操作和命令. 阅读本文大概需要15分钟,通过阅读本文你将知道一下 ...
- spring cloud 总结
Finchley版本Spring Cloud Finchley; Spring Boot 2.0.3 史上最简单的 SpringCloud 教程 | 第一篇: 服务的注册与发现(Eureka)(Fin ...
- cdnbest常见http状态码解释
cdnbest的常见状态码提示界面: 400状态码:语义有误,当前请求无法被服务器理解 网站访问报这个提示:host not found(找不到主机),这是没有同步站点信息到这个节点上,检查节点连接主 ...
- dagScheduler
由一个action动作触发sparkcontext的runjob,再由此触发dagScheduler.runJob,然后触发submitJob,封装一个JobSubmitted放入一个队列.然后再通过 ...
- web设计工具
1.工具 WYSIWYG_Web_Builder_12 2.网页 https://bootstrapstudio.io/#purchase