Entity Framework 6.0 Tutorials(2):Async query and Save
Async query and Save:
You can take advantage of asynchronous execution of .Net 4.5 with Entity Framework. EF 6 has the ability to execute a query and command asynchronously using DbContext.
Let's see how to execute asynchronous query first and then we will see an asynchronous call to context.SaveChanges.
Asynchronous Query:
private static async Task<Student> GetStudent()
{
Student student = null; using (var context = new SchoolDBEntities())
{
Console.WriteLine("Start GetStudent..."); student = await (context.Students.Where(s => s.StudentID == ).FirstOrDefaultAsync<Student>()); Console.WriteLine("Finished GetStudent..."); } return student;
}
As you can see in the above code, GetStudent method is marked with async to make it asynchronous. The return type of asynchrounous method must be Task. GetStudent returns an object of Student entity so return type must be Task<Student>.
Also, query is marked with await. This frees the calling thread to do something else until it executes the query and returns the data. We have used FirstOrDefaultAsync extension method of System.Data.Entity. You may use other extension methods appropriately, such as SingleOrDefaultAsync, ToListAsyn, etc.
Asynchronous Save:
You can call context.SaveChanges asynchronously the same way as async query:
private static async Task SaveStudent(Student editedStudent)
{ using (var context = new SchoolDBEntities())
{
context.Entry(editedStudent).State = EntityState.Modified; Console.WriteLine("Start SaveStudent..."); int x = await (context.SaveChangesAsync()); Console.WriteLine("Finished SaveStudent...");
} }
Getting async query result:
You can get the result when asynchronous using the wait method as below:
public static void AsyncQueryAndSave()
{
var student = GetStudent(); Console.WriteLine("Let's do something else till we get student.."); student.Wait(); var studentSave = SaveStudent(student.Result); Console.WriteLine("Let's do something else till we get student.." ); studentSave.Wait(); }
As shown in the code above, we call async method GetStudent in the usual way and store the reference in the variable student. Then, we call student.wait(). This means that the calling thread should wait until the asynchronous method completes, so we can do another process, until we get the result from the asynchronous method.
The code shown above will have the following output:
Download sample project for Async query & save demo.
Entity Framework 6.0 Tutorials(2):Async query and Save的更多相关文章
- Entity Framework 6.0 Tutorials(1):Introduction
以下系统文章为EF6.0知识的介绍,本章是第一篇 原文地址:http://www.entityframeworktutorial.net/entityframework6/introduction.a ...
- Entity Framework 6.0 Tutorials(4):Database Command Logging
Database Command Logging: In this section, you will learn how to log commands & queries sent to ...
- Entity Framework 6.0 Tutorials(11):Download Sample Project
Download Sample Project: Download a sample project for Entity Framework 6 Database-First model below ...
- Entity Framework 6.0 Tutorials(10):Index Attribute
Index Attribute: Entity Framework 6 provides Index attribute to create Index on a particular column ...
- Entity Framework 6.0 Tutorials(9):Stored Procedure Mapping
Code First - Insert, Update, Delete Stored Procedure Mapping: Entity Framework 6 Code-First provides ...
- Entity Framework 6.0 Tutorials(6):Transaction support
Transaction support: Entity Framework by default wraps Insert, Update or Delete operation in a trans ...
- Entity Framework 6.0 Tutorials(3):Code-based Configuration
Code-based Configuration: Entity Framework 6 has introduced code based configuration. Now, you can c ...
- Entity Framework 6.0 Tutorials(8):Custom Code-First Conventions
Custom Code-First Conventions: Code-First has a set of default behaviors for the models that are ref ...
- Entity Framework 6.0 Tutorials(7):DbSet.AddRange & DbSet.RemoveRange
DbSet.AddRange & DbSet.RemoveRange: DbSet in EF 6 has introduced new methods AddRange & Remo ...
随机推荐
- matlab与modelsim中的文件操作函数
matlab中 fscanf和fpintf是一对,用fprintf写的必须用fscanf来读. fread和fwrite是一对,用fwrite写的必须用fread来读. 同样的数据,使用fprintf ...
- 【eclipse】 怎么解决java.lang.NoClassDefFoundError错误
前言 在日常Java开 发中,我们经常碰到java.lang.NoClassDefFoundError这样的错误,需要花费很多时间去找错误的原因,具体是哪个类不见了?类 明明还在,为什么找不到?而且我 ...
- gatsbyjs 了解
1. 模型 2. 总结&&资料 从模型上可以看出和jamstack 提出的架构模型比较相似,可以看成是一个具体的实现,功能还是比较强大的 https://www.gatsbyjs.o ...
- java 执行JavaScript 以及容器化的问题
1. 可选方案 a. jdk 6 开始内置的Rhino 引擎 b. jdk8 替换的nashorn (性能高,对于ECMA 支持更好) c. java 版的nodejs vertx.i ...
- C#防止程序多次运行
经过我的测试,还比较好用,但是有个问题,如果不注销,用另一个用户进入,则程序不能判断出已运行.所以只限于用在单用户环境,还是不太完美. class Program { [STAThread] stat ...
- Python函数-repr()
repr(object) 作用: repr() 函数将对象转化为供解释器读取的形式. object --对象.返回一个对象的 string 格式. 实例: >>>s = 'RUNOO ...
- spring切面配置,代理用jdk和cglib的区别
jdk的动态代理大家应该都听说过,条件是必须要有接口:cglib不要求接口,那么它是怎么实现切面的呢?很简单,通过继承,它动态的创建出一个目标类的子类,复写父类的方法,由此实现对方法的增强.看例子: ...
- vs2005中microsoft ado data control 6.0控件问题
在vs2005中是没有这个控件的,需要注册,步骤如下: 1. 先到C:\WINDOWS\system32目录下看看你的系统里是否已经有了MSADODC.ocx和MSDATGRD.ocx这两个文件(多半 ...
- erlang配置三方库
暴力的: 直接下载解压以后放到erlang的lib目录,比如/usr/local/Cellar/erlang/17.3/lib/erlang/lib 和谐的: 在用户名下建立.erlang文件 在里面 ...
- python开发mysql:索引
一,索引管理 索引分类: 普通索引INDEX:加速查找 唯一索引: -主键索引PRIMARY KEY:加速查找+约束(不为空.不能重复) -唯一索引UNIQUE:加速查找+约束(不能重复) 联合索引: ...