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 ...
随机推荐
- Python字符串格式化 (%操作符)
在许多编程语言中都包含有格式化字符串的功能,比如C和Fortran语言中的格式化输入输出.Python中内置有对字符串进行格式化的操作%. 模板 格式化字符串时,Python使用一个字符串作为模板.模 ...
- leetcode101
/** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNo ...
- leetcode124
class Solution { int maxValue; public int maxPathSum(TreeNode root) { maxValue = Integer.MIN_VALUE; ...
- 悲观锁,乐观锁,排他锁,行锁----MYSQL
在说具体的锁结构时,先思考一个问题,那就是为什么要上锁?然后我要如何选择锁?锁具体如何实现? 在文章得末尾我给出了我的个人答案. 一.什么是悲观锁? 1.悲观锁就是在操作数据时,认为此操作会出现数据冲 ...
- “AS3.0高级动画编程”学习:第三章等角投影(上)
什么是等角投影(isometric)? 原作者:菩提树下的杨过出处:http://yjmyzz.cnblogs.com 刚接触这个概念时,我也很茫然,百度+google了N天后,找到了一些文章: [转 ...
- Linux firewalld使用教程+rhce课程实验
--timeout= 设置规则生效300秒 调试阶段使用,防止规则设置错误导致无法远程连接 实验:在server0机器上部署httpd服务,通过添加富规则,只允许172.25.0.10/32访问,并且 ...
- 机器学习(二)--------单变量线性回归(Linear Regression with One Variable)
面积与房价 训练集 (Training Set) Size Price 2104 460 852 178 ...... m代表训练集中实例的数量x代表输入变量 ...
- git连接远程客户端,命令行窗口上传文件
1.git官网,下载安装git客户端 2.配置全局的name和email,生成key git config --global user.name XXX git config --global us ...
- 时间戳转中国人能看得懂的日期格式 yy-mm-dd
很多项目都会用到时间戳的转换 说实话 我现在的这家公司超级好 因为后太要求传数据的时候竟然可以是时间戳的格式 我觉得我好幸福 哈哈哈 不过 等后台转给你数据的时候很多时候都是时间戳 这时候就得前端转 ...
- org.hibernate.InvalidMappingException: Could not parse mapping document from resource com/domain/book.hbm.xml 联网跑一跑
org.hibernate.InvalidMappingException: Could not parse mapping document from resource com/domain/boo ...