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 ...
随机推荐
- 【java规则引擎】规则引擎RuleBase中利用观察者模式
(1)当RuleBase中有规则添加或删除,利用观察者模式实现,一旦有变动,规则引擎其他组件也做出相应的改变.(2)学习思想:当一个应用中涉及多个组件,为了实现易扩展,解耦思想.可以利用观察者模式实现 ...
- 前端工程师面试问题归纳(二、问答类JQ相关)
其他随笔 前端工程师面试问题归纳(一.问答类html/css/js基础) 前端工程师面试问题归纳(三.代码类) 1. jQuery 库中的 $() 是什么? $() 函数是 jQuery() 函数的别 ...
- OpenSSH 使用技巧
1. 取消 OpenSSH 初次连接 yes 确认 在脚本中有时会使用ssh进行远程连接操作,如果是第一次 ssh 连接往往会提示你是否确认连接并要求你输入yes, 才能继续.如何才能避免这个步骤呢? ...
- new Date(2016,3,29,10) 时区的问题
http://my.oschina.net/xinxingegeya/blog/394821http://www.cnblogs.com/qiuyi21/archive/2008/03/04/1089 ...
- DataSnap侦听端口动态设置问题
使用DataSnap做服务器,端口设置为可配置,然后在程序中动态指定: procedure TscUPower.DataModuleCreate(Sender: TObject); begin dst ...
- Centos7手工安装Kubernetes集群
安装Kubernetes集群有多种方式,前面介绍了Kubeadm的方式,本文将介绍手工安装的方法. 安装环境有3台Azure上的VM: Hkube01:10.0.1.4 Hkube02:10.0.1. ...
- java实现一个最简单的tomcat服务
在了解tomcat的基本原理之前,首先要了解tomcatt最基本的运行原理. 1.如何启动? main方法是程序的入口,tomcat也不例外,查看tomcat源码,发现main是在Bootstrap ...
- Dell PowerEdge R720内存安装原则
Dell PowerEdge R720内存安装原则 摘要:系 统包含 24 个内存插槽,分为两组(每组 12 个),每个处理器一组.每组的 12 个插槽分入四个通道.在每个通道中,第一个插槽的释放 ...
- java.sql.SQLException: No suitable driver
java.sql.SQLException: No suitable driver at java.sql.DriverManager.getDriver(Unknown Source) at com ...
- AngularJS:表达式
ylbtech-AngularJS:表达式 1.返回顶部 1. AngularJS 表达式 AngularJS 使用 表达式 把数据绑定到 HTML. AngularJS 表达式 AngularJS ...