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 ...
随机推荐
- sqlserver float小数存数据库变成多位了 比如说12.23存进去变成 12.229999998 甚至更长
使用 numeric(12,2)的数据类型,或者decimal(12,2) 追问 不能随意修改表结构 有别人办法么 程序上控制的 追答 那你就不用管他了,所谓 浮点数,必然是这么存储的.
- JavaScript学习-4——DOM对象、事件
本章目录 --------window对象 --------document对象 --------事件 一.window对象 函数调用: 自己封装的函数只写:函数名(): 数学函数Math 例:绝对值 ...
- git 提交小备注
总结: · git add -A 提交所有变化 · git add -u 提交被修改(modified)和被删除(deleted)文件,不包括新文件(new) · git add . 提交 ...
- php状态设计模式
状态设计模式的关键就是,环境中拥有所需的全部状态对象,每个状态对象又引用了环境对象:环境对象通过维护一个当前状态属性(用于存放状态对象)从而对所需的全部状态对象产生影响. 下面演示了一个简单的状态设计 ...
- Linux命令:readonly
readonly [-aAf] [name[=value] ...] or readonly -p -A 表示后面的name变量都是关联数组 -a 表示后面的name变量都是index数组 -f 表示 ...
- html5移动端查找
用form包裹住input,修改input的类型为seach,然后给input绑定seach事件,当输入状态是输入键盘上会出现搜索,点击搜索就可以查找了 <form action="& ...
- Java学习笔记(十九):Object类
- jQuery开发工具
开发工具:MyEclipse2014 + aptana插件 下载apada 放到MyEclipse的路径 https://segmentfault.com/a/1190000005711923 ...
- C源文件和头文件 模版
头文件: /********************************************************************************************** ...
- cast
https://blog.csdn.net/seabeam/article/details/47841539 在UVM中经常可以看到$cast的身影,这是SV的build-in task之一,当然它还 ...